Cant change directory between loops
1 view (last 30 days)
Show older comments
Joe Beard
on 14 Mar 2022
Edited: Walter Roberson
on 15 Mar 2022
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
0 Comments
Accepted Answer
Walter Roberson
on 14 Mar 2022
Edited: Walter Roberson
on 15 Mar 2022
img = fullfile(dataset(i).folder,dataset(i).name);
2 Comments
More Answers (2)
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.
0 Comments
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
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
See Also
Categories
Find more on Image Data Workflows 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!