"Cannot open file" C: "for reading, you may not have read permission." Error in MATLAB
I have code like this:
for x = 1:100
path = sprintf('C:\Users\hasan_000\Documents\MATLAB\Project\Images\%d.jpg', x);
imgarray = imread(sprintf(path));
end
I have a folder of 100 images. I want to convert them to a matrix by automatically loading them in a loop.
But I am getting this error:
Cannot open file "C:" for reading; you may not have read permission.
How can I fix the problem?
Thank,
source to share
The code should display a warning:
"Warning: play sequence '\ U' is invalid. See" help sprintf "for a valid sequence escape code."
When using sprintf, you need to avoid \
. With the code yor path
is C:
. For example, for how escaping is done correctly, check the documentation at sprintf
. I would use this code instead:
P=fullfile('C:\Users\hasan_000\Documents\MATLAB\Project\Images',sprintf('%d.jpg',x))
imgarray = imread(P);
source to share
sprintf('C:\\Users\\hasan_000\\Documents\\MATLAB\\Project\\Images\\%d.jpg', x);
should solve the problem.
Sprintf ('% s% d% s' ,: 'JPG' 'C \ Users \ hasan_000 \ Documents \ MATLAB \ Project \ Images \', x,);
- this is what I would like to suggest as it makes the code more readable and understandable.
source to share
sprintf
don't like your backslashes \
in the filename as it might be part of a specific command. If you just run the path file, you will see:
path = sprintf('C:\Users\hasan_000\Documents\MATLAB\Project\Images\%d.jpg', 1);
path = C:
So where is your code breaking. I am not currently sitting on a Windows machine, but I would try to reverse the backslashes from backslashes \
to normal /
and see if it can open that.
The second method works exactly:
path = ['C:\Users\hasan_000\Documents\MATLAB\Project\Images\', sprintf('%d.jpg', x)]
path = C: \ Users \ hasan_000 \ Documents \ MATLAB \ Project \ Images \ 1.jpg
source to share