function [filenames,imagelist]=CheckPMexist(PMdirectory,InputDirectory,InputFilenames, ImagesDir);
% re-afirm the list of image files
%imagelist=[];
% files = dir([ImagesDir, filesep, '*.jpg']);
% InputFilenames = {files.name};
% %for i=1:size(InputFilenames,1)
%     for i=1:length(InputFilenames)
% 
%         imagename=dir([InputDirectory,deblank(InputFilenames{i})]);
% 
%     %imagename=dir([InputDirectory,deblank(InputFilenames(i,:))]);
%     if ~isempty(imagename)
%      %  imagelist=strvcat(imagelist,InputFilenames(i,:));
%                 imagelist=strvcat(imagelist,InputFilenames{i});
% 
%     end
% end
%filenames=[];
%imagelist4=[];
%k=0;
imagelist = [];
filenames = [];
for i=1:length(InputFilenames)
    %imname=deblank(imagelist(i,:));
    imname = InputFilenames{i};
    [pmpath, pmname, ext] = fileparts(imname);
    pmname = [PMdirectory, filesep, pmname,'_pm.mat'];
    if exist(pmname)
        imagelist = strvcat(imagelist, imname);
        filenames = strvcat(filenames, pmname);
        
    end
%     d=dir([PMdirectory,name_pm]); %deblank(opts.InputFilenames(i,:))]);
%     if (~isempty(d))%&(rem(i,2)==0) % WATCH OUT
%         filenames=strvcat(filenames,name_pm);
%         imagelist4=strvcat(imagelist4,tempimagename);
%         imagelist2(i,:)=[imagelist(i,:),' 1'];
%     else
%         imagelist2(i,:)=[imagelist(i,:),' 0'];
%         k=k+1;
%         imagelist3(k,:)=[imagelist(i,:),' 0'];
%     end
end
%excelfilename=[InputDirectory,'Files_used.xls'];
%xlswrite(excelfilename,imagelist,1);
%xlswrite(excelfilename,imagelist3,2);