Cant change directory between loops

1 view (last 30 days)
I am trying to change files between loops and read in the images from each, however only the images from the first file are ever read in. Apologies if this has been asked before, I couldnt find anything similar when looking for a solution.
for j = 1:5
ds = sprintf("J:\\Images\\BCS %d", j);
dsn = dir(ds);
dataset=dsn(~ismember({dsn.name},{'.','..'}));
for i = 1:length(dataset)
img = dataset(i).name;
imgr = imread(img);
imshow(imgr)
stats = regionprops(imgr, 'all');
allPerimeters = [stats.Perimeter];
allAreas = [stats.Area];
allCircularities(i, j) = allPerimeters .^ 2 ./ (4 * pi* allAreas);
end
end

Accepted Answer

Walter Roberson
Walter Roberson on 14 Mar 2022
Edited: Walter Roberson on 15 Mar 2022
img = fullfile(dataset(i).folder,dataset(i).name);
  2 Comments
Stephen23
Stephen23 on 15 Mar 2022
img = fullfile(dataset(i).folder,dataset(i).name);
^^^

Sign in to comment.

More Answers (2)

Jon
Jon on 14 Mar 2022
Edited: Jon on 14 Mar 2022
Looking at your loops the logic seems basically OK but I can't run it to see exactly what happens as I don't have your image directories.
I would suspect that you are not really getting back a full list of all of the files in each directory you are looping through. So in particular you should check that the results of
dsn = dir(ds);
are what you expect. If not look one line earlier and see whether the string ds is really what you want. It looks like it evaluates to something like, for example with j = 3
ds = "J:\Images\BCS 3"
Is that the name of a folder full of images?
Note when providing code you can use the code button in the Answers toolbar to format it nicely.
Also when you do your directory search you can use wildcard characters, like J:\Images\myImages\*.jpg to just get jpegs or whatever.Then you don't have to get rid of the . and .. in your results.

yanqi liu
yanqi liu on 15 Mar 2022
for j = 1:5
ds = sprintf("J:\\Images\\BCS %d", j);
dsn = dir(ds);
dataset=dsn(~ismember({dsn.name},{'.','..'}));
for i = 1:length(dataset)
if dataset(i).isdir == 1
continue;
end
img = fullfile(dataset(i).folder,dataset(i).name);
imgr = imread(img);
figure(1); clf; imshow(imgr, [])
stats = regionprops(imgr, 'all');
allPerimeters = [stats.Perimeter];
allAreas = [stats.Area];
allCircularities{i, j} = allPerimeters .^ 2 ./ (4 * pi* allAreas);
end
end
  2 Comments
Walter Roberson
Walter Roberson on 15 Mar 2022
Edited: Walter Roberson on 15 Mar 2022
for j = 1:5
ds = sprintf("J:\\Images\\BCS %d", j);
dsn = dir(ds);
dataset = dsn(~[dsn.isfolder]); %also deals with . and ..
for i = 1:length(dataset)
img = fullfile(dataset(i).folder,dataset(i).name);
imgr = imread(img);
figure(1); clf; imshow(imgr, [])
stats = regionprops(imgr, 'all');
allPerimeters = [stats.Perimeter];
allAreas = [stats.Area];
allCircularities{i, j} = allPerimeters .^ 2 ./ (4 * pi* allAreas);
end
end
Stephen23
Stephen23 on 15 Mar 2022
dataset = dsn(~[dsn.isfolder]);
% ^^^

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!