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

조회 수: 113 (최근 30일)
AK
AK 2021년 3월 14일
댓글: AK 2021년 3월 15일
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?
AK
AK 2021년 3월 15일
Thank you! It is working now!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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