Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
merging existing .fig into the same figure

Subject: merging existing .fig into the same figure

From: Ziv

Date: 21 May, 2010 21:24:04

Message: 1 of 14

Hi,
I have two figures e.g. gugu.fig and lulu.fig )
i want to place tham one next to the other and create gugululu.fig plot where gugu is on the right and lulu is on the left.

can i call gugu and lulu from subplot? (wasn't able to do that).

I've been reading some suggestions in this site, dealing with figure properties, copyobj, etc'. for some reason it didn't work (

I'd appreciate an example for creating such a merge.
thanks,
ZiV

Subject: merging existing .fig into the same figure

From: Yi Cao

Date: 21 May, 2010 21:49:05

Message: 2 of 14

"Ziv " <ziv.yekutieli@hotmail.com> wrote in message <ht6tlk$k4t$1@fred.mathworks.com>...
> Hi,
> I have two figures e.g. gugu.fig and lulu.fig )
> i want to place tham one next to the other and create gugululu.fig plot where gugu is on the right and lulu is on the left.
>
> can i call gugu and lulu from subplot? (wasn't able to do that).
>
> I've been reading some suggestions in this site, dealing with figure properties, copyobj, etc'. for some reason it didn't work (
>
> I'd appreciate an example for creating such a merge.
> thanks,
> ZiV

One way to do this is to extract xdata and ydata from existing figures, then re-plot these data in a new figure window as desired. For example,

open('gugu.fig');
h_gugu=get(gca,'Children');
x_gugu=get(h_gugu,'XData');
y_gugu=get(h_gugu,'YData');

open('lulu.fig');
h_lulu=get(gca,'Children');
x_lulu=get(h_lulu,'XData');
y_lulu=get(h_lulu,'YData');

figure
subplot(121)
plot(x_gugu,y_gugu);
subplot(122)
plot(x_lulu,y_lulu)
saveas(gcf,'gugululu','fig')

HTH
Yi

Subject: merging existing .fig into the same figure

From: Ziv

Date: 22 May, 2010 20:02:04

Message: 3 of 14

"Yi Cao" <y.cao@cranfield.ac.uk> wrote in message <ht6v4h$mt2$1@fred.mathworks.com>...
> "Ziv " <ziv.yekutieli@hotmail.com> wrote in message <ht6tlk$k4t$1@fred.mathworks.com>...
> > Hi,
> > I have two figures e.g. gugu.fig and lulu.fig )
> > i want to place tham one next to the other and create gugululu.fig plot where gugu is on the right and lulu is on the left.
> >
> > can i call gugu and lulu from subplot? (wasn't able to do that).
> >
> > I've been reading some suggestions in this site, dealing with figure properties, copyobj, etc'. for some reason it didn't work (
> >
> > I'd appreciate an example for creating such a merge.
> > thanks,
> > ZiV
>
> One way to do this is to extract xdata and ydata from existing figures, then re-plot these data in a new figure window as desired. For example,
>
> open('gugu.fig');
> h_gugu=get(gca,'Children');
> x_gugu=get(h_gugu,'XData');
> y_gugu=get(h_gugu,'YData');
>
> open('lulu.fig');
> h_lulu=get(gca,'Children');
> x_lulu=get(h_lulu,'XData');
> y_lulu=get(h_lulu,'YData');
>
> figure
> subplot(121)
> plot(x_gugu,y_gugu);
> subplot(122)
> plot(x_lulu,y_lulu)
> saveas(gcf,'gugululu','fig')
>
> HTH
> Yi

Hi Yi,
first, thanks for the prompt response, i can already get something going )
some questions still:
when i run:
>open('gugu.fig');
>h_gugu=get(gca,'Children');
i get:
h_gugu =
  172.3331
  171.3347

why the two numbers?
if i delete one of them i can get some progress and get the two subplots.

do i get it right that i need to copy each and every property of the fig file? i.e. lines, colormap, titles etc'. is there a way to copy all together?

thanks a lot for your help,
ZiV

Subject: merging existing .fig into the same figure

From: Matt Fig

Date: 22 May, 2010 20:17:04

Message: 4 of 14

This should work. You will get more than one child if there is more than one thing plotted or an annotation, etc.



gu = open('gugu.fig');
gu_ax = gca;
lu = open('lulu.fig');
lu_ax = gca;

figure; % The new figure
P = subplot(1,2,2);
copyobj(get(gu_ax,'children'),P)
P = subplot(1,2,1);
copyobj(get(lu_ax,'children'),P)

Subject: merging existing .fig into the same figure

From: Ziv

Date: 23 May, 2010 10:55:06

Message: 5 of 14

"Matt Fig" <spamanon@yahoo.com> wrote in message <ht9e40$5p4$1@fred.mathworks.com>...
> This should work. You will get more than one child if there is more than one thing plotted or an annotation, etc.
>
>
>
> gu = open('gugu.fig');
> gu_ax = gca;
> lu = open('lulu.fig');
> lu_ax = gca;
>
> figure; % The new figure
> P = subplot(1,2,2);
> copyobj(get(gu_ax,'children'),P)
> P = subplot(1,2,1);
> copyobj(get(lu_ax,'children'),P)

Getting there ) but still something is missing:
if the axes in lulu and gugu are not the same, for some reason i get the same axes in both subplots. I try to do the folllowing in order to change the axes seperatly but it doesn't work fine:
Plu = subplot(1,2,1);
PPlu=get(Plu,'Parent');
copyobj(get(lu,'children'),PPlu)

I seem to override both subplots with lulu...

Subject: merging existing .fig into the same figure

From: Matt Fig

Date: 23 May, 2010 15:43:03

Message: 6 of 14

What do you mean, "If the axes in lulu and gugu are not the same"? Are you talking about their limits or their labels or their colors or their grid status or what?

If you are talking about something which has a handle, like the xlabel, ylabel, and title, you can simply add a line in the code I gave you for each subplot:


P = subplot(1,2,2);
T = get(gu_ax,{'title';'xlabel';'ylabel'}); % Get the title, xlabel and ylabel.
copyobj([get(gu_ax,'children');[T{:}]'],P)

Now any other properties you want copied, you could do like this:

C = {'xlim';'ylim';'color'}; % Cell of properties to copy
set(P,C,get(gu_ax,C)) % And likewise for lu_ax etc.

Subject: merging existing .fig into the same figure

From: Matt Fig

Date: 23 May, 2010 15:57:03

Message: 7 of 14

I don't know why I didn't think of it before, but this should completely copy everything:

% Open old figures.
gu = open('gugu.fig');
gu_ax = gca;
lu = open('lulu.fig');
lu_ax = gca;



F = figure; % New figure
P1 = subplot(1,2,1); % Plot a subplot.
P1_pos = get(P1,'position'); % get its position.
delete(P1) % Delete the subplot
P2 = subplot(1,2,2);
P2_pos = get(P2,'position');
delete(P2);
P = copyobj(gu_ax,F); % Copy the gu_ax to new fig
set(P,'position',P2_pos) % Set its position to the deleted subplot's position, duh!
P = copyobj(lu_ax,F);
set(P,'position',P1_pos)

Subject: merging existing .fig into the same figure

From: Ziv

Date: 23 May, 2010 21:17:02

Message: 8 of 14

Hi,
thanks again for the prompt response, now it looks a lot better and i think by this progress i have much better understanding of how these "families" should be handled ) so any gaps will be fixed soon.
thanks a lot!
ZiV

Subject: merging existing .fig into the same figure

From: Ziv

Date: 24 May, 2010 15:50:25

Message: 9 of 14

Hi, if you already took the time to write this code, i'd appreciate the opportunity to learn from you since you seem to know your way around Matlab very well. can you please explain the line:

copyobj([get(gu_ax,'children');[T{:}]'],P)

how does [T{:}]' interperted?

thanks,
ZiV

Subject: merging existing .fig into the same figure

From: us

Date: 24 May, 2010 16:05:23

Message: 10 of 14

"Ziv " <ziv.yekutieli@hotmail.com> wrote in message <hte781$oo5$1@fred.mathworks.com>...
> Hi, if you already took the time to write this code, i'd appreciate the opportunity to learn from you since you seem to know your way around Matlab very well. can you please explain the line:
>
> copyobj([get(gu_ax,'children');[T{:}]'],P)
>
> how does [T{:}]' interperted?
>
> thanks,
> ZiV

typical syntax to turn a CELL array of numbers into a COL vec

     t={10,1:5}
     [t{:}].'
%{
% ans =
    10
     1
     2
     3
     4
     5
%}

us

Subject: merging existing .fig into the same figure

From: Walter Roberson

Date: 24 May, 2010 16:16:01

Message: 11 of 14

Ziv wrote:

> how does [T{:}]' interperted?

Equivalent code:

temp = []
for K = 1 : numel(T)
   temp = [temp T{K}];
end
temp = temp ';


It is, in other words, the same as writing

[T{1} T{2} T{3} ... T{end}]'

Warning: ' is the complex conjugate transpose operator, not the plain
transpose operator, which is .' in Matlab.

Subject: merging existing .fig into the same figure

From: Yair Altman

Date: 24 May, 2010 22:05:07

Message: 12 of 14

> I have two figures e.g. gugu.fig and lulu.fig )
> i want to place tham one next to the other and create gugululu.fig plot where gugu is on the right and lulu is on the left.

The previous answers to this request involved opening the figures and copying the data (via get) or the objects themselves (via copyobj) into the third figure that combines them. Actually, the source figures do not need to become visible at all, if you use the hgload() function, as follows:

hFig = hgload(figFilename, struct('visible','off'));

Fortunately, there is a hack that you can use to get these objects without even needing to open the figures. The trick relies on the undocumented fact that FIG files are simply MAT files that contain all the HG (Handle Graphics) information in a very simple struct. You can simply read the contents of these FIG files using the load(figFileName,'-mat') function, and then use the struct2handle() function to place the requested handle(s) anywhere in the new figure.

Yair Altman
http://UndocumentedMatlab.com

Subject: merging existing .fig into the same figure

From: Chi-Fu

Date: 13 Jan, 2014 05:24:08

Message: 13 of 14

Hi Matt,
This works great! thank you.
One little question is that how to show both figure legend?
Thanks,
Yen

"Matt Fig" wrote in message <htbj8f$7kv$1@fred.mathworks.com>...
> I don't know why I didn't think of it before, but this should completely copy everything:
>
> % Open old figures.
> gu = open('gugu.fig');
> gu_ax = gca;
> lu = open('lulu.fig');
> lu_ax = gca;
>
>
>
> F = figure; % New figure
> P1 = subplot(1,2,1); % Plot a subplot.
> P1_pos = get(P1,'position'); % get its position.
> delete(P1) % Delete the subplot
> P2 = subplot(1,2,2);
> P2_pos = get(P2,'position');
> delete(P2);
> P = copyobj(gu_ax,F); % Copy the gu_ax to new fig
> set(P,'position',P2_pos) % Set its position to the deleted subplot's position, duh!
> P = copyobj(lu_ax,F);
> set(P,'position',P1_pos)

Subject: merging existing .fig into the same figure

From: Doruk

Date: 8 Apr, 2014 06:52:10

Message: 14 of 14

I just created an account to thank for this solution: Thank you.

"Matt Fig" wrote in message <htbj8f$7kv$1@fred.mathworks.com>...
> I don't know why I didn't think of it before, but this should completely copy everything:
>
> % Open old figures.
> gu = open('gugu.fig');
> gu_ax = gca;
> lu = open('lulu.fig');
> lu_ax = gca;
>
>
>
> F = figure; % New figure
> P1 = subplot(1,2,1); % Plot a subplot.
> P1_pos = get(P1,'position'); % get its position.
> delete(P1) % Delete the subplot
> P2 = subplot(1,2,2);
> P2_pos = get(P2,'position');
> delete(P2);
> P = copyobj(gu_ax,F); % Copy the gu_ax to new fig
> set(P,'position',P2_pos) % Set its position to the deleted subplot's position, duh!
> P = copyobj(lu_ax,F);
> set(P,'position',P1_pos)

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us