How to merge two figure files into a single file
Asked Answered
C

3

6

This should be a problem with a trivial solution, but still I wasn't able to find one.

Say that I have 2 matlab figures fig1.fig, fig2.fig which I want to load and show in the same plotting window.

What should I do?

I mean, I am pretty sure that I can accomplish the task using some low(er) level graphic command which extracts contents from one image and put them in the second one, nonetheless I cannot believe that there is not any high level function (load fig2 on top of fig1) that does this...Comparing 2 plots (unfortunately already saved) is a very common task, I'd say.

Convertite answered 7/11, 2012 at 18:38 Comment(5)
It is a common task, but the usual way to accomplish it is to replot the data (at least in my experience.) Any reason you can't regenerate the figures?Adelina
.fig files are already there; they are produced by different codes whose results I want to compare. They take a while to run, more than figuring out a way to manipulate graphics. :(Convertite
Hmmm, the other trick is to store the relevant data to files, and read it later. Realize that may not apply to your situation.Adelina
What do you mean by compare? Do you want to compute numbers on the data in the figures, or do you want to combine the plots into a single figure?Wooley
both of the files contain a standard x-f(x) plot of some physical quantities. What I need is what I would obtain doing plot(x,f1(x));hold on;plot(x,f2(x));, (possibly changing the style of lines). Now I just need to show them, thus no numerical comparison is needed. I understand that if I needed numerical comparison Re-running the simulations would have been necessary.Convertite
W
18

Its not clear if you want to extract data from the figures and compare the data, or if you want to combine the plots from two figures into a single figure.

Here is how you combine two figures into one (if thats what you want to do)..

First load the figures:

fig1 = open('FigureFile1.fig');
fig2 = open('FigureFile2.fig');

Get the axes objects from the figures

ax1 = get(fig1, 'Children');
ax2 = get(fig2, 'Children');

Now copy the hangle graphics objects from ax2 to ax1. The loop isn't neccesary if your figures only have a single axes

for i = 1 : numel(ax2) 
   ax2Children = get(ax2(i),'Children');
   copyobj(ax2Children, ax1(i));
end

Note This example assumes that your figures have the same nubmer of axes and that you want to copy objects from the first axes in the second figure to the first axes on the first figure. Its up to you to figure out the proper indexing if the axes indices aren't lined up.

Wooley answered 7/11, 2012 at 18:54 Comment(4)
I guess I am in the Note case.. one of the two figures has a 2x1 subplot.Convertite
@Convertite additionally you can grab the data from the objects in the figures and then create an entirely new figure from that data, its a bit more work but it will give you flexibilityWooley
Thanks, I'm figuring out the way of using this get-children mechanism to obtain nicer things.. Definitely what I needed. Thanks indeed.Convertite
@slayton:Hello, can you tell me this please?If i run the above code i am taking 2 plots.I want to take 1 plot ( 2 plots merged in 1)Lauricelaurie
D
9

The answer slayton gave is good. Here's another tip: If you have two plots opened in two separate Matlab figure windows, don't forget you can point-and-click copy the proper plots. Do this by clicking the arrow pointer in the Matlab figure window, and then clicking on the plotted line. Copy the (plotted line, textbox, etc...) object. Then, similarly select the axis in the other Matlab figure window and paste it.

I give this 'silly' solution because it has proven to be useful in in collaboration meetings. Point-and-click copying in front of someone (like your adviser) communicates exactly what curves are being compared, and it prevents you from having to fire up code in front of others.

Dirac answered 7/11, 2012 at 19:11 Comment(0)
A
2

You can also go to File in the menu, Generate Code, for each plots. Then copy and paste both in the same mfile, with a "hold on" in between and changing details related to the appearance. Then run the new m-file.

Adjunction answered 1/1, 2014 at 21:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.