Subplot disappeared after getting its handle
    6 views (last 30 days)
  
       Show older comments
    
    Shao-Yu Wang
 on 6 May 2023
  
    
    
    
    
    Commented: Shao-Yu Wang
 on 21 May 2023
            I have a figure consisting of five subplots and I want to save or open one of them in another figure. For some reason, whenever I tried to get the handle with the first line. That subplot specifically just turned empty and the new figure contains only an empty axis too. Any help on selecting a subplot would be appreciated.
h_subplot = subplot(5,1,2);
figure;
h_newaxis = copyobj(h_subplot, gcf);
set(h_newaxis, 'Position', get(0,'DefaultAxesPosition'));
0 Comments
Accepted Answer
  Constantino Carlos Reyes-Aldasoro
      
 on 9 May 2023
        Ok, I have now seen that the error is in the object that you are trying to copy, it should NOT be the subplot, but the actual object that you display in that subplot. Try the following:
figure
h1_subplot = subplot(5,1,2);h1_ax = imagesc(rand(16));
figure
h2_subplot = subplot(5,1,4);
h2_newaxis = copyobj(h1_ax,h2_subplot );
Hope that now the problem should be solved.
3 Comments
  Constantino Carlos Reyes-Aldasoro
      
 on 19 May 2023
				In that case, you will need to find it through handles and children. For example, take this code:
figure
subplot(3,2,1); plot(1:10,1:10,'bo');
subplot(3,2,2); plot(1:20,1:20,'rx');
subplot(3,2,3); plot(1:30,1:30,'md');
subplot(3,2,4); plot(1:40,1:40,'ys');
subplot(3,2,5); plot(1:50,1:50,'gd');
Now, we use gcf to get the figure and store this in the handle h0
h0=gcf;
get(h0)
As you can see there are 5 children in the handle, you can then access these
h1 = h0.Children(2);
get(h1)
You will need to play a bit to find which is the axes that you want to copy, but this will work. 
If you want to know more about handles and how to create publication quality images, I can recommend my book:
Biomedical Image Analysis Recipes in MATLAB: For Life Scientists and Engineers
https://www.wiley.com/en-sg/Biomedical+Image+Analysis+Recipes+in+MATLAB%3A+For+Life+Scientists+and+Engineers-p-9781118657553
Hope this solves your problem, or at least points you in the right direction.
More Answers (1)
  Constantino Carlos Reyes-Aldasoro
      
 on 6 May 2023
        The problem is the order in which you are passing the commands, you create a subplot in a figure, then you call for a new figure and then you use gcf. Try like this
h_fig   = figure;
h_subplot = subplot(5,1,2);
h_newaxis = copyobj(h_subplot, h_fig);
set(h_newaxis, 'Position', get(0,'DefaultAxesPosition'));
See Also
Categories
				Find more on Subplots 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!

