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

60 次查看(过去 30 天)
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

采纳的回答

Steven Lord
Steven Lord 2021-3-14
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 个评论
Rik
Rik 2021-3-15
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?

请先登录,再进行评论。

更多回答(1 个)

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

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by