Here's a solution that stays close to your current one:
A = dir(fullfile(a,'*.jpg'));
B = dir(fullfile(a,'*.tif'));
fileNames = {A.name,B.name};
for iFile = 1:numel(fileNames)
newName = fullfile(a,sprintf('%0d.tif',iFile));
movefile(fullfile(a,fileNames{iFile}),newName);
end
Where a
is the folder you have defined already.
UPDATE
The file extension '.jpg' and '.tif' are just labels indicating the image format expected within that file. There are just labels though and can be wrong. For example if you gave these files a '.txt' extension they do not suddenly become text files, though you are more likely to be able to open them up in a text editor and verify.
That being said, many image loaders will first verify the image format directly from the file (and not the extension) and use the corresponding codec to load it. So the mismatch in format and file extension may not actually matter in practice, but you are relying a bit on chance.
You can transcode your jpeg formatted images to tiff format directly however. A similar question was asked at The Mathworks site.
Essentially you would read the file and then write it in the new format. Here's how you can change the above code to do it:
A = dir(fullfile(a,'*.jpg'));
B = dir(fullfile(a,'*.tif'));
fileNames = {A.name,B.name};
for iFile = 1:numel(fileNames)
[~,~, fileExt] = fileparts(fileNames{iFile});
if(strcmpi(fileExt,'.jpg'))
fullJpegFilename = fullfile(a,fileNames{iFile});
tmpImg = imread(fullJpegFilename); % read jpeg format
imwrite(tmpImg,fullfile(a,sprintf('%0d.tif',iFile)); % write file as .tif format
delete(fullJpegFilename); % no longer needed
else
newName = fullfile(a,sprintf('%0d.tif',iFile));
movefile(fullfile(a,fileNames{iFile}),newName);
end
end