Filter löschen
Filter löschen

The directory of simulation

4 Ansichten (letzte 30 Tage)
Zeynab Mousavikhamene
Zeynab Mousavikhamene am 2 Nov. 2019
Bearbeitet: Walter Roberson am 2 Nov. 2019
I am using this code to get all images in a folder by user:
d = uigetdir(pwd, D);
imagefiles = dir(fullfile(d, '*.jpg'));
nfiles = length(imagefiles);
Then in a for loop I will do some calculations on the images:
k = 1:nfiles
currentfilename = imagefiles(k).name;
I = imread(currentfilename);
.
.
.
when running the code I get this error:
Error using imread>get_full_filename (line 568)
File "1.jpg" does not exist.
Error in imread (line 377)
fullname = get_full_filename(filename);
Error in DemoBoundary29sept2018edit3ReadsImagesfromfolder (line 30)
I = imread(currentfilename)
But 1.jpg exists in that folder. I think the reason is that the folder that the code is saved is different from folder that the images are.
Any idea how to solve the issue?

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 2 Nov. 2019
Bearbeitet: Walter Roberson am 2 Nov. 2019
currentfilename = fullfile(d, imagefiles(k).name);
The .name field returned by dir() never includes folder information: it is the basic file name (including extension) only.
Since R2015b or so, there is also a .folder field, so
currentfilename = fullfile(imagefiles(k).folder, imagefiles(k).name);
could be used. That is useful if you used wildcards in folder specifications.
I find it easier to attach the folder names before-hand:
filenames = fullfile(d, {imagefiles.name});
and then extract filenames{k} and use that.

Weitere Antworten (0)

Kategorien

Mehr zu File Operations finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by