Clear Filters
Clear Filters

Unable to draw files from a folder

2 views (last 30 days)
Varun Wadia
Varun Wadia on 10 Oct 2018
Edited: Stephen23 on 11 Oct 2018
I am trying to draw tiff image files from a folder on my computer. The relevant folder is in my path and the code is
myfolder = 'C:\Users\varunwadia\Documents\MATLAB\Objects';
files = fullfile(myfolder, '*.tiff');
photos = dir(files);
'photos' should now be a 1x2000 struct with the fields 'name', 'folder', 'date', 'bytes' etc. and when I run this code on my laptop (changing the address for 'myfolder' appropriately) it is. However, on my PC it is a 0x1 struct. I am absolutely sure I've assigned 'myfolder' with the correct value and the 'objects' folder has tiffs in it. What am I missing?

Accepted Answer

Stephen23
Stephen23 on 11 Oct 2018
Edited: Stephen23 on 11 Oct 2018
This will get all the filenames, remove the filenames starting with ._, sort the filenames into alphnumeric order, and then import them inside the loop:
D = 'directory where the files are stored';
S = dir(fullfile(D,'*.tiff'));
N = {S.name};
X = strncmp(N,'._',2);
N = natsortfiles(N(~X)); % optional: download from FEX.
C = cell(1,numel(N);
for k = 1:numel(N)
F = fullfile(D,N{k});
I = imread(F);
... your code
C{k} = ... store any results.
end
If you want the filenames in numeric order, you can download natsortfiles from here:
The code in my answer is based on the examples in the MATLAB documentation:

More Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!