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

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

 Akzeptierte Antwort

Steven Lord
Steven Lord am 14 Mär. 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 Kommentare

Thank you!
Im new to matlab, so how do i use fullfile() here?
My code now:
dataset = dir('/Users/Desktop/Image Dataset');
dataset = dataset(~[dataset.isdir]);
numFiles = length(dataset);
fullFileName = fullfile(folder, 'file');
for k = 1:numFiles
mydata{k} = imread(fullfile(dataset(k).name));
axes(handles.axes1)
imshow(mydata{k})
Im getting this error:
Error using imread>get_full_filename (line 570)
File ".DS_Store" does not exist.
Did you read the documentation for fullfile? The Matlab documentation is one of the major advantages over competition, so I strongly suggest using it as your first stop if you don't understand something.
Thank you, yes, I have read it.
From my understanding, this should work:
dataset = dir(fullfile('/Users/Desktop/Image Dataset'));
dataset = dataset(~[dataset.isdir]);
numFiles = length(dataset);
fullFileName = fullfile(folder, 'file');
for k = 1:numFiles
mydata{k} = imread(dataset(k).name);
axes(handles.axes1)
imshow(mydata{k})
end
But I still recieve this error:
Error using imread>get_full_filename (line 570)
File ".DS_Store" does not exist.
Could you please indicate what im doing wrong?
Thanks!
fullfile creates a valid path from folder and file names. Since you only have 1 input to it when you call dir, it doesn't do much there.
In the loop itself you only provide the file name itself, not the complete path. You can generate the complete path by using fullfile:
full_path=fullfile(main_folder,sub_folder,sub_sub_folder,filename);
%or, in your case:
full_path=fullfile(dataset(k).folder,dataset(k).name);
P = matlabroot
P = '/MATLAB'
F = 'myfun.m'
F = 'myfun.m'
PF = fullfile(P, F)
PF = '/MATLAB/myfun.m'
Think about how you could use this inside your loop.
AK
AK am 15 Mär. 2021
Bearbeitet: AK am 15 Mär. 2021
I understand now, thank you, that i was only giving the individual file name- without the path to the file. So I have tried, as suggested, this inside the loop:
for k = 1:numFiles
full_path=fullfile(dataset(k).folder, dataset(k).name);
mydata{k} = imread(full_path);
However this causes this error:
Error using imread>get_format_info (line 545)
Unable to determine the file format.
So I'm guessing you filtered out the directories as I called out in my second problem. But this code is going to iterate over all the files in this directory, and I'm guessing you have a mix of image and non-image files in the directory. What happens if I try to imread something that's not an image?
try
F = which('ode45.m');
imread(F)
catch ME
fprintf("The imread call threw the following error:\n%s\n", ME.message)
fprintf("The error identifier for this error is:\n%s\n", ME.identifier)
end
The imread call threw the following error: Unable to determine the file format.
The error identifier for this error is: MATLAB:imagesci:imread:fileFormat
MATLAB (correctly) can't treat the ode45.m program file as an image. So you need to narrow the list of files even further, or wrap the imread call in a try / catch block and choose to ignore messages with that particular identifier. In that latter case rethrow any other error message so if there's a different error it will still cause your code to stop execution.
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?
Thank you! It is working now!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

PABISH
PABISH am 5 Okt. 2024

0 Stimmen

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

Kategorien

Tags

Gefragt:

AK
am 14 Mär. 2021

Beantwortet:

am 5 Okt. 2024

Community Treasure Hunt

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

Start Hunting!

Translated by