Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge two figure files into a single file

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.

like image 519
Acorbe Avatar asked Nov 07 '12 18:11

Acorbe


People also ask

How do I join two plots in MATLAB?

Combine Plots in Same Axes However, you can use the hold on command to combine multiple plots in the same axes. For example, plot two lines and a scatter plot. Then reset the hold state to off. When the hold state is on, new plots do not clear existing plots or reset axes properties, such as the title or axis labels.


2 Answers

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.

like image 124
slayton Avatar answered Oct 05 '22 09:10

slayton


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.

like image 45
Sam Avatar answered Oct 05 '22 07:10

Sam