How can I repeat the same algorithm from different folders?

1 view (last 30 days)
Hi, I have this algorithm which works perfectly. However, it only reads one folder at a time and graphs me only one curve at a time (1 FOLDER= 1 PLOT AS RESULT OF THE ANALYSIS). I would like to be able to read 4 other folders, do the same operations and then graph all five curves in the same graph.
This is the algorithm that only reads one folder at a time:
%this is for the destination of the folder
srcFile = dir('C:\Users\agnes\Pictures\POST PROCESSING 4\PP4 TGC-MED RD 20\*.dcm');
pathname = ('C:\Users\agnes\Pictures\POST PROCESSING 4\PP4 TGC-MED RD 20\');
%All the operations below must be repeated for each folder. Stored in different variables if you like.
% The important thing is that the same curves that I find individually (by changing the destination of the folder)
% I can graph them together at the end
numberofimages = 21; %I define the number of the image for each folder
numberofroi = 6; %each image has to be crop 6 times
%Work on the reference images
I=dicomread('21'); %dicom read works just as imread (but for diagnostic images)
R = nan(numberofroi,4);
for nr = 1:numberofroi
h = drawrectangle(gca); wait(h);
roibasket = cell(numberofroi,numberofimages);
meanbasket = nan(size(roibasket));
for ni = 1:numberofimages
for nr = 1:numberofroi
roibasket{nr,ni} = imcrop(pileofimages,R(nr,:));
meanbasket(nr,ni) = mean( pileofimages(masks{nr}) ); %meanbasket is a matrix that I need for each folder
%I need this operation for each folder
rvalue = [-9; -6; -3; 3; 6; 9];
errbasket = mean(meanbasket - rvalue);
hold on
%In the command below I have plotted the curve of only the first result
%I need to plot all the curves in the same plot
figure; plot(errbasket);
The five folder to analyze are:
C:\Users\agnes\Pictures\POST PROCESSING 4\PP4 TGC-MED RD 20\
C:\Users\agnes\Pictures\POST PROCESSING 4\PP4 TGC-MED RD 40\
C:\Users\agnes\Pictures\POST PROCESSING 4\PP4 TGC-MED RD 60\
C:\Users\agnes\Pictures\POST PROCESSING 4\PP4 TGC-MED RD 80\
C:\Users\agnes\Pictures\POST PROCESSING 4\PP4 TGC-MED RD 100\
HelpAStudent on 1 Oct 2021
Yeah each folder has 21 items. I want to save from each folder the mean (meanbasket) and the error (errbasket) and plot all the errbasket together
HelpAStudent on 1 Oct 2021
Aniway your program does not read now the reference image, I think because It does not recognise the destination. Even if I put inside dicomread something like that
I=dicomread('C:\Users\agnes\Pictures\POST PROCESSING 4\PP4 TGC-MED RD\21.dcm');

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 1 Oct 2021
To get a list of all dcm files in all subfolders below some top level folder, use the "star star" wildcard in the dir() function.
topLevelFolder = 'C:\Users\agnes\Pictures\POST PROCESSING 4\PP4 TGC-MED RD 20\';
filePattern = fullfile(topLevelFolder, '**\*.dcm')
fileList = dir(filePattern)
% Loop over them.
numFiles = length(fileList);
for k = 1 : numFiles
thisFileName = fullfile(fileList(k).folder, fileList(k).name);
fprintf('File #%d of %d :\n "%s"\n', k, numFiles, thisFileName);
% Optional : Get size info if desired:
d = dir(thisFileName);
fprintf(' It is %d bytes.\n', d.bytes);
% Now call imread() or whatever you want to do.
HelpAStudent on 1 Oct 2021
The code just open:
fileList =
0×1 empty struct array with fields:
It does not work
Image Analyst
Image Analyst on 1 Oct 2021
Then you do not have any DCM files under that folder. Try the root folder:
topLevelFolder = 'C:\'
It might take a while to scan your whole hard disk though.

Sign in to comment.

More Answers (0)


Find more on Loops and Conditional Statements 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!