Cannot open file "." for reading. You might not have read permission.

55 views (last 30 days)
I am trying to loop through a file and read each image in turn. However, it keeps saying I dont have read permission.
How do I fix this? Thank you!
dataset = dir('/Users/Desktop/Image Dataset');
numFiles = length(dataset);
mydata = cell(1,numFiles);
for k = 1:numFiles
mydata{k} = imread(dataset(k).name);
axes(handles.axes1)
imshow(mydata{k})
end

Accepted Answer

Steven Lord
Steven Lord on 14 Mar 2021
dataset = dir('/Users/Desktop/Image Dataset');
All the entries in dataset (including the '.' and '..' directories) are relative to "/Users/Desktop/Image Dataset".
numFiles = length(dataset);
mydata = cell(1,numFiles);
for k = 1:numFiles
mydata{k} = imread(dataset(k).name);
There are at least two problems with this line.
1) You're not trying to read the data files from "/Users/Desktop/Image Dataset", you're trying to read them from somewhere on the MATLAB search path. Use the fullfile function to include the path name in the input to imread. This contributes partly to MATLAB being unable to find the file. But the main reason is:
2) You're assuming all the entries in dataset refer to image files. That output from dir will contain at least two elements that aren't, the aforementioned '.' and '..' directories. To resolve this you can prune all entries that refer to directories from dataset before calculating numFiles.
dataset = dataset(~[dataset.isdir]);
Continuing on with the rest of your code:
axes(handles.axes1)
imshow(mydata{k})
Are you sure you want to do this? Unless this is a simplified version of your problem, the image in mydata{1} will at most flicker on the screen for as long as it takes MATLAB to read the data for the second file. [In actuality, it probably won't even show for that long since there's no drawnow command in your loop.]
end
  9 Comments
Rik
Rik on 15 Mar 2021
Use the debugger: what is the value of full_path at the point the error occurs? Can you see what could cause Matlab to be confused about the file format?

Sign in to comment.

More Answers (1)

PABISH
PABISH on 5 Oct 2024
when i tried running this simulation it shows, file cannot be opened for reading. what should i do?

Categories

Find more on File Operations in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!