get folder names in a directory

79 views (last 30 days)
Happy PhD
Happy PhD on 5 Apr 2023
Edited: Stephen23 on 5 Apr 2023
Hi, I am trying to get the names of the folders within a certain folder representing a distance, to upload the images within each distance folder.
To go through all the folders one by one and upload the image to do some calculation ebfore doing to the next folder. Each folder contains a number which respresent a distance.
for example
> measurement data
>> 705
>>> image1.png
>>> bg.png
>>706
>>> image2.png
>>> bg.png
etc.
I would like to determine which distances we have in the measurement data folder, and for each distance folder, .i.e. 705and 706, then go into 705 folder and upload the images image1.png and bg.png, do some calculation and then repeat the same thing with 706 folder. Note that the images names can be different, but it will always cotain 'image' and 'bg' in the title names.
I tried something like;
cd(foldername);
Files = dir;%('*.png');
content = dir(foldername);
ImageFiles = strings(length(content),1);
BGFiles = strings(length(content),1);
FileName = strings(length(content),1);
i = 1;
for k=1:length(content)
if content(k).isdir == 1
value = content(k).name;
if value(1) == '.'
% do nothing
else
ImageFiles(k) = value;
FileName(k) = Files(k).name;
i = i+1;
end
end
end
DistStr = strings(length(ImageFiles),1);
for k = 1:length(ImageFiles)
tmp = ImageFiles(k);
DistStr(k) = string(tmp(1));
if contains(DistStr(k), 'c')
DistStr(k) = replace(DistStr(k),'c', '.');
end
i = i + 1;
end
i = 0;
for k = 1:length(DistStr)
if strlength(DistStr(k)) ~= 0
i= i + 1;
end
end
Distances = ones(i,1);
for k = 1:i
Distances(k) = str2double(DistStr(k));
end
Distances = Distances(~isnan(Distances));
FileName = FileName(~isnan(Distances));
i get the following content:
content.name
ans =
'.'
ans =
'..'
ans =
'705'
ans =
'706'
ans =
'Settings_230403_161032.txt'
ans =
'Summary_230404.txt'
K>>
Distances doesn't turn up since 705 and 706 is at position 3 and 4 in DistStr. Distances is empty when I run the code.
Can anyone help?
  1 Comment
Stephen23
Stephen23 on 5 Apr 2023
Edited: Stephen23 on 5 Apr 2023
"I am not sure the number of '.' will be the same for all folders. so skipping the first two is not a sure thing for all cases."
You should not assume that the dot directory name will always exist or that they will always be the first two listed:
As Walter Roberson wrote in that last link: "In short: if your code assumes that '.' and '..' are the first two entries in a directory, your code has a bug (even in MS Windows). If your code assumes that directory entries are returned in any sorted order, your code has a bug (in all OS.)"
Assuming that the dot directory names are first (or that they exist at all) creates bugs in code: e.g. any change to the filenames, foldernames, or to the DIR search string will break the code. The recommended approaches are e.g. ISMEMBER or SETDIFF to remove the dot directory names, which are both robust and work in those different situations.
The simplest and most robust approach is to avoid the situation by simply specify something non-empty in the DIR search string, so that the dot directory names are not even returned in the first place.

Sign in to comment.

Answers (1)

Stephen23
Stephen23 on 5 Apr 2023
Edited: Stephen23 on 5 Apr 2023
Do NOT use CD just to access data files: absolute/relative filenames are more efficient and more robust.
Rather than getting the names of absolutely everything in those folders, let DIR do more of this task for you., i.e. by providing a suitable search name (which should be an absolute/relative filename).
This code assumes that your have exactly two such matching images file in each subfolder:
S1 = dir(fullfile(foldername,'*','*image*.png'));
S2 = dir(fullfile(foldername,'*','*bg*.png'));
assert(isequal({S1.folder},{S2.folder}),'File number/locations do not match.')
for k = 1:numel(S1)
F1 = fullfile(S1(k).folder,S1(k).name);
F2 = fullfile(S2(k).folder,S2(k).name);
% do whatever with each filename
end

Categories

Find more on File Operations in Help Center and File Exchange

Tags

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!