Clear Filters
Clear Filters

Subplot with matlab figures (.fig files)

12 views (last 30 days)
Javier Abanades
Javier Abanades on 28 Mar 2013
if true
% code
endDear all,
I would like to plot 4 figures in the same plot using the function subplot.
I read this code on the website for 2 figures...
h1 = openfig('test1.fig','reuse'); % open figure
ax1 = gca; % get handle to axes of figure
h2 = openfig('test2.fig','reuse');
ax2 = gca;
s1 = subplot(2,1,1); %create and get handle to the subplot axes
s2 = subplot(2,1,2);
fig1 = get(ax1,'children'); %get handle to all the children in the figure
fig2 = get(ax2,'children');
copyobj(fig1,s1); %copy children to new parent axes i.e. the subplot axes
copyobj(fig2,s2);
I want to insert 4 figures, so I re-edit the code, but it did not work with the last figure, what is wrong with it?
h1 = openfig('154 withour.fig','reuse'); % open figure
ax1 = gca; % get handle to axes of figure
h2 = openfig('154 with.fig','reuse');
ax2 = gca;
h3 = openfig('315 without.fig','reuse'); % open figure
ax3 = gca; % get handle to axes of figure
h4 = openfig('315 with.fig','reuse'); % open figure
ax4 = gca; % get handle to axes of figure
% test1.fig and test2.fig are the names of the figure files which you would % like to copy into multiple subplots
s1 = subplot(2,2,1); %create and get handle to the subplot axes
s2 = subplot(2,2,2);
s3 = subplot(2,2,3);
s4 = subplot(2,2,4);
fig1 = get(ax1,'children'); %get handle to all the children in the figure
fig2 = get(ax2,'children');
fig3 = get(ax3,'children');
fig4 = get(ax4,'children');
copyobj(fig1,s1); %copy children to new parent axes i.e. the subplot axes
copyobj(fig2,s2);
copyobj(fig3,s3);
copyobj(fig4,s4);
  3 Comments
Javier Abanades
Javier Abanades on 3 Apr 2013
Error using handle.handle/get Invalid or deleted object.
Error in Multiplot (line 21) fig4 = get(ax4,'children');
But I cannot understand why the function get does not work with the fig4 if it works with the rest of figures....
Matlab prints the first figure, the second and the third one but with the four fails...
I do not know why... any idea?
Regards Javier

Sign in to comment.

Answers (4)

Javier Abanades
Javier Abanades on 3 Apr 2013
This is the error I get...
Error using handle.handle/get Invalid or deleted object.
Error in Multiplot (line 21) fig4 = get(ax4,'children');
I do not understand why works for every figure and get an error for the last one

julien
julien on 3 Apr 2013
if you want to plot 4 figues subplot(4,4,1) plot( T, f1(T)); %% fig 2 subplot(4,4,2) plot( T, f2(T));
%%%fig 3 subplot(4,4,3) plot( T, f3(T));
%% fig 4 subplot(4,4,4) plot( T, f4(T));

Javier Abanades
Javier Abanades on 3 Apr 2013
But you need to open the figures first and select the axis of the figure.
Besides, if you use subplot (4,4,1) you will charge 16 spaces for the figures...

Mike Garrity
Mike Garrity on 5 Apr 2013
Just guessing, but I don't see you creating a fifth figure after loading the four. Therefore, I would guess that the first call to subplot is deleting ax4.

Community Treasure Hunt

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

Start Hunting!