Search code examples
imagematlabimage-processingsvmimread

"Can't open file "C:" for reading; you may not have read permission." error in MATLAB


I have such a code;

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 involves 100 pictures. I want to convert them to matrix by uploading automatically in a loop.

But I get this error:

Can't open file "C:" for reading;
you may not have read permission.

How can I fix the problem?

Thanks,


Solution

  • The code should output the warning:

    "Warning: Escape sequence '\U' is not valid. See 'help sprintf' for valid escape sequences. "

    You need to escape the \ when using sprintf. With yor code path is C:. For examples how proper escaping is done, please check the documentation for sprintf. Instead I would use this code:

    P=fullfile('C:\Users\hasan_000\Documents\MATLAB\Project\Images',sprintf('%d.jpg',x))
    imgarray = imread(P);