MATLAB: Unable to read certain .JPG files in a folder

.jpgimread

Hello,
I am having a similar problem to this post: unable-to-read-image-jpg-file
Basically, I have a loop that uses imread to find .jpg files then applies a rotation to the image. The loop worked fine when I was testing with a small batch of images. Now, when I add new .jpg files, I get the error "Error using imread" and that certain files were not found. I tested this with 'exist' and get the output 2. Based on the documentation this means: "2 — name is a file with extension .m, .mlx, or .mlapp, or name is the name of a file with a non-registered file extension (.mat, .fig, .txt)." How can I move forward? Why are some .jpg files being read while some are not?
Here is the relevant portion of my code:
display ('top');
%Get list of all files in the folder with the desired file name pattern
filePattern = fullfile(myFolder, '*.jpg'); %find files that are .jpg
jpegFiles = dir(filePattern); %dir = list all files that match .jpg (from filePattern)
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
display ('before loop');
for k = 1:length(jpegFiles) %loop through all .jpg files
display ('inside loop');
baseFileName = jpegFiles(k).name; %name of .jpgs (indexed individually (k)); Ex: SDC10705_RS.jpg
fullFileName = fullfile(myFolder, baseFileName); %fullfile builds filename from parts = myfolder\baseFileName (full path of ^)
fprintf(1, 'Now reading %s\n', fullFileName);
imageArray = imread(fullFileName); %read specific FULLfilename from store of .jpg files
%this should be the full
%Rotate
display ('rotation');
%raw= jpegFiles(k).name;
I = imread(jpegFiles(k).name); %must have imread for next step to work (rotation of I)
R = imrotate(I, 355, 'bicubic'); %put angle and type of rotation here. 'bicubic' works best/ least distortion
imshow(R); %display image
%drawnow; %force display to update immediately
%Break up fileparts to allow saving with '_rotate' extension when looping through files
[tmp_path, tmp_name, tmp_ext] = fileparts(fullFileName); %separating fullFileName, into path, name, and ext (.jpg) (pretty much opposite of fullfile)
%need to do this so .jpg is at the end
fullFileName_rotate = [tmp_path, filesep, tmp_name, '_rotate',tmp_ext];
imwrite(R, fullFileName_rotate); %new rotated image
%saved as filename_rotate
end;
Thank you.

Best Answer

Most likely, the line

imageArray = imread(fullFileName); %read specific FULLfilename from store of .jpg files

works fine for all the files. However, you're getting an error with the next imread:

I = imread(jpegFiles(k).name);

because that one looks for the file in the local folder instead of myFolder.

I = imread(fullFileName);

would fix the issue, but I don't see the point of reloading an image that you've already loaded in memory, so the simplest is to get rid of that imread and use the existing imageArray instead of I in the next line:

R = imrotate(imageArray, 355, 'bicubic'); %put angle and type of rotation here. 'bicubic' works best/ least distortion