views:

45

answers:

3

Hi,

My program produces small figures during the command cycle. Is there a way to just save these figures to then combine them in one figure later?

So then how do you load them into a single figure later?

Best,

A: 

Use saveas. Save your subplot as a FIG file so you have complete control over it later (as opposed to a JPG).

Choose a tiling pattern and then use subplot to display multiple figures in one.

Jacob
How do you load them then into a single figure?
Vass
@Vass: I've updated my answer
Jacob
+1  A: 

Hi,

I have an answer here as an example:

h1=figure(1)
plot(1:10,'o-r');
title('title');
xlabel('xlabel');
ylabel('ylabel');
% copy contents
ch(1)=copyobj(gca,gcf);
% fig 2
h2=figure(2)
plot(1:30,'o-r');
title('title fig2');
xlabel('xlabel');
ylabel('ylabel');
% copy contents
ch(2)=copyobj(gca,gcf);

figure(3)
sh=subplot(1,2,1);
clear axes
p=get(sh,'position');
ah=copyobj(ch(1),gcf);
set(ah,'position',p);

% ... create axis template
sh=subplot(1,2,2);
clear axes
p=get(sh,'position');
ah=copyobj(ch(2),gcf);
set(ah,'position',p);
% ... delete template
% delete(sh);

best,

Vass
A: 

Consider the code:

hFig = figure;

%# create temporary subplots as template
for i=1:2, h(i) = subplot(2,1,i); end       %# create subplots
pos = get(h, 'Position');                   %# record their positions
delete(h)                                   %# delete them

%# load the .fig files inside the new figure
fileNames = {'a.fig' 'b.fig'};              %# saved *.fig file names
for i=1:2
    %# load fig
    hFigFile = hgload( fileNames{i} );

    %# move/copy axis from old fig to new fig
    hAx = get(hFigFile, 'Child');           %# hAx = gca;
    set(hAx, 'Parent',hFig)
    %#hAx = copyobj(hAx,hFig);

    %# resize it to match subplot position
    set(hAx, 'Position',pos{i});

    %# delete old fig
    delete(hFigFile)
end

This was adapted from this newsgroup discussion

Amro

related questions