Subplot in loop returns images in separate figures
    4 views (last 30 days)
  
       Show older comments
    
    Paul Costache
 on 16 Aug 2021
  
    
    
    
    
    Answered: Walter Roberson
      
      
 on 16 Aug 2021
            Hello.
I am running this loop and I want to save the results after each loop and show them in the same subplot, but I get for example in this case 4 different figures with the corresponding image. I would like to get only one figure with the 4 subplots. Thanks for your help!
n=4;
nrows = n/2; % number of subplot rows f. subplot mit alle maskedRGBImage
ncols = n/2; % number of subplot columns
nsubs = nrows * ncols; % total number of subplots
for     f = 1:n
f4=figure('Name','Bilder vor und nach Bearbeitung');
hold(subplot(nrows, ncols,f), 'on');
subplot(nrows, ncols, f)
imshow(maskedRGBImage);
%this part shows the object idx on top of the object 
textFontSize2 = 8;	% Used to control size of "particle number" labels put atop the image.
labelShiftX = -140 ;	% Used to align the labels in the centers of the objects.
allBlobCentroids = [stats2.Centroid];
centroidsX = allBlobCentroids(1:2:end-1);
centroidsY = allBlobCentroids(2:2:end);
% Index in Mitte der Partikel anzeigen; top to bottom, left to right
for k = 1 : numberOfparticles          % Loop through all particles.
	text(centroidsX(k) + labelShiftX, centroidsY(k), num2str(k), 'FontSize', textFontSize2, 'FontWeight', 'Bold','Color','red');
end
%
title(['Masked Image' num2str(f)]);
drawnow;
end
0 Comments
Accepted Answer
  Walter Roberson
      
      
 on 16 Aug 2021
        f4=figure('Name','Bilder vor und nach Bearbeitung');
However, figure(f) and figure(n) are the only two syntaxes that return existing figures. When you pass in any parameter other than f (a figure handle) or n (an integer figure number) then it is either an error or else a new figure is created.
If you are trying to retrieve an existing figure, then record f4 before the for loop, and if necessary then
figure(f4)
0 Comments
More Answers (0)
See Also
Categories
				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!