How to plot multiple figures (.fig) into one figure?

65 views (last 30 days)
Dear Community member, Im new to MATLAB and in need of your assistant. I have different plots which i saved as figures using saveas function and .fig format. Is it possible to somehow take the figures from each folder and then make one plot? the setup of the figures(standard 2d x-y plot) are exactly the same but each figure contains "circle" points and a curve which is based on a model. but with different values ofcourse. I want to show the differences of these "curve" fits. I have for example: model1.fig model2.fig
one friend suggested "hold on" but im not sure how to since im really really new to MATLAB. your help will be much appreciated. Thank you in advance Best Regards Saad Khan
  1 Comment
AMIT MALGOL
AMIT MALGOL on 3 May 2019
f=openfig('model1.fig');
H=findobj(f,'Type','line');
x_data=get(H,'xdata');
y_data=get(H,'ydata');
g=openfig('model2.fig');
G=findobj(g,'Type','line');
x1_data=get(G,'xdata');
y1_data=get(G,'ydata');
figure(1)
xlabel('x');
ylabel('y');
plot(x_data,y_data);
hold on
plot(x1_data,y1_data);

Sign in to comment.

Answers (1)

Geoff Hayes
Geoff Hayes on 6 Apr 2017
Saad - you could try opening the figure and then grab the handles for the graphics object (or objects) that were drawn on the axes (of that figure). For example, suppose we do
x = -2*pi:0.01:2*pi;
y = sin(x);
h = plot(x,y);
saveas(h,'mySinePlot.fig');
So we have drawn the familiar sine curve and then saved the figure to a file. Now, we open the figure as
close all; % ensure existing figures are closed
open mySinePlot.fig;
If we assume that there is just one child object (for the figure) and that it is the axes, then we could do
hFigAxes = get(gcf,'Children');
We now grab the children for the axes. Given our very simple example, there will be just one child from which we can get the x and y data that we used when calling plot
hSinePlot = get(hFigAxes,'Children');
xData = get(hSinePlot,'XData');
yData = get(hSinePlot,'YData');
If you do the same for the other figure, then you will be able to obtain both sets of x- and y-data of which you can then plot onto the same figure.
Your plots may not be as simple as the above, but start with this code and see what happens. If your axes has more than one child, then loop through all of the children until you find the one that you are looking for (if a plot, then its type is Line).

Categories

Find more on Printing and Saving in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!