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)
imshow(I)
R = nan(numberofroi,4);
masks=cell(numberofroi,1);
for nr = 1:numberofroi
h = drawrectangle(gca); wait(h);
R(nr,:)=h.Position;
masks{nr}=h.createMask;
end
roibasket = cell(numberofroi,numberofimages);
meanbasket = nan(size(roibasket));
for ni = 1:numberofimages
for nr = 1:numberofroi
filename=(num2str(ni));
pileofimages=dicomread(strcat(pathname,filename));
info=dicominfo(strcat(pathname,filename));
roibasket{nr,ni} = imcrop(pileofimages,R(nr,:));
meanbasket(nr,ni) = mean( pileofimages(masks{nr}) ); %meanbasket is a matrix that I need for each folder
end
end
%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\
  4 Comments
Agnese Chini
Agnese Chini 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.
end
  2 Comments
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)

Community Treasure Hunt

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

Start Hunting!