How to arrange several shapes in an array of subtitles while preserving all the properties?

Imagine that I have a function myPlotthat creates a simple graph and returns a shape and axis:

function [ fig_handle, axes_handle ] = myPlot( myTitle )

x = linspace(-10,10,100);
y = x.^2;

fig_handle = figure;
plot(x,y,'Color','b')
ylim([0,42]); xlim([-42/10,42/10]);
ylabel('y'); xlabel('x');
title(myTitle);
grid on;
axes_handle = gca;

end

Now I want to call this function several times with different input parameters and combine them into an array of subheadings. The solution I came up with is

[f1,a1] = myPlot('Plot #1');
[f2,a2] = myPlot('Plot #2');

figure(3)
s221 = subplot(211);
s222 = subplot(212);
copyobj(get(a1,'children'),s221)
copyobj(get(a2,'children'),s222)

it gives me

enter image description here

therefore, the new two-part plot does not preserve the properties of the two graphs before. Of course, I know that I can just do:

set(s221,'Ylabel',get(a1,'Ylabel')) 

with all the properties. But I try to avoid this. Is there something simpler what am I missing?

+4
source share
2 answers

( ) . , , - subplot (xyz).

:

[f1,a1] = myPlot('Plot #1');
[f2,a2] = myPlot('Plot #2');

h4 = figure(4)
copyobj(get(f1,'children'),h4)
copyobj(get(f2,'children'),h4)

, . .


, subplot , ( ), , ( subplot.m, ).

%// Just to get some position calculated for me
figure(3)
s221 = subplot(211);
s222 = subplot(212);

:

hl = flipud( get(h4,'Children') ) ;
set( hl(1),'Position', get(s221,'Position') )
set( hl(2),'Position', get(s222,'Position') )

The result:

, / ( flipud), , .


(f1 f2), ( ), Parent axes, ( ). :

set(a1,'Parent',h4) ; close(f1)
set(a2,'Parent',h4) ; close(f2)

copyobj. . , , , (Matlab ... ), , .

+4

, . .

myPlot, .

function [ fig_handle, axes_handle ] = myPlot(fig_handle, sub, myTitle )

figure(fig_handle); subplot(sub)

....

end

f = figure(3);
[f,a1] = myPlot(f, 221,'Plot #1');
[f,a2] = myPlot(f, 222,'Plot #2');
+1

All Articles