MATLAB Answers

Combine three or more MATLAB figures

161 views (last 30 days)
Tomer on 31 Aug 2019
Commented: Tomer on 15 Oct 2019
Hello, I am new to MATLAB. I have three MATLAB figures. I want to combine them to compare the data. Can someone help me doing this?


Show 5 older comments
Adam Danz
Adam Danz on 4 Oct 2019
Option 1 is to merely save the data instead of plotting the data. Once you have both datasets, just plot them together on 1 plot.
Option 2 is to use copyobj() to copy all of the children from one axis to another.
Option 3 is the grab the XData and YData from each graphic object and to use those data to produce a new plot.
Tomer on 5 Oct 2019
Unfortunately, I can't use these options. I have four matlab figures (4 different cases). I want to combine them and plot them all as a single figure.
Adam Danz
Adam Danz on 5 Oct 2019
Both of those options as well as option #3 (which I just added) do exactly what you're describing. It sounds like you'd rather work with the previously created figures rather than with the code that produced the figures. If that's the case, option 2 and 3 are viable options, unless I'm not understanding the problem.
For option 2, you merely need to copy the line objects from 3 of the 4 figures on to the 4th figure (or copy objects from all 4 figs to a new figure). That can be done manually or programmatically (copyobj).
For option 3, you just need to get the handles to your line objects and then access the XData and YData (and ZData?). Then you can use those data to create a new plot. Note that depending on what you're plotting, you may want to access a different set of data from the handle.
If you need specific help with that, we'll need more info and perhaps a copy of the figure.

Sign in to comment.

Accepted Answer

meghannmarie on 4 Oct 2019
I would save figures then plot them as subplots like this:
%First Figure
h1 = openfig('test1.fig','reuse'); % open figure
ax1 = gca; % get handle to axes of figure
%Second Figure
h2 = openfig('test2.fig','reuse');
ax2 = gca;
h3 = figure; %create new figure
s1 = subplot(1,2,1); %create and get handle to the subplot axes
s2 = subplot(1,2,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


Show 1 older comment
meghannmarie on 6 Oct 2019
Do you want them on one axis?
fig = figure();
ax = axes(fig);
hold on
h1 = openfig('test1.fig','reuse');
h2 = openfig('test2.fig','reuse');
h3 = openfig('test3.fig','reuse');
Adam Danz
Adam Danz on 6 Oct 2019
@Tomer, meghannmarie 's demo above demonstrates my option #2.
h1 is the figure handle to test1.fig.
h1.Children will list the axes and any other object stored on that figure. If There's just 1 axes and nothing else, you can use this demo code directly. Otherwise you'll need to add an additional step to locate the axis (that's easy - we could help you with that). Then you want to get the children of the axis which will be the handles to any graphic objects stored on the axes. They will then be copied to the new axes.

Sign in to comment.

More Answers (0)

Translated by