How can I repeat the same algorithm from different folders?
Show older comments
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
Geoff Hayes
on 1 Oct 2021
@Agnese Chini - do each of the folders have the same number of files (21)? You could just wrap the above code in a loop and on each iteration, read the data from a different folder. Then plot the data to the figure. I see you are using hold on already which you will need to retain each plot to the same figure, but you will only need to create one figure (so move the figure call to just before you start the outer loop).
Kevin Holly
on 1 Oct 2021
What variables, if any, do you want to save that change between folders? Let me know and I can adjust the script.
Are all folders located in the same folder? If so, a list of the folder names can be generated, which can be used.
figure;
numberofimages = 21; %Is this value different in each folder?
numberofroi = 6;
for i = [20:20:100]
%this is for the destination of the folder
srcFile = dir(['C:\Users\agnes\Pictures\POST PROCESSING 4\PP4 TGC-MED RD ' num2str(i) ' \*.dcm']);
pathname = ('C:\Users\agnes\Pictures\POST PROCESSING 4\PP4 TGC-MED RD ' num2str(i) '\');
I=dicomread('21');
% 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}) );
end
end
%I need this operation for each folder
rvalue = [-9; -6; -3; 3; 6; 9];
errbasket = mean(meanbasket - rvalue);
plot(errbasket);hold on
end
HelpAStudent
on 1 Oct 2021
HelpAStudent
on 1 Oct 2021
Accepted Answer
More Answers (0)
Categories
Find more on Lighting, Transparency, and Shading 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!