MATLAB Answers

Load figures from .fig files and copy into subplots including legends and axis labels

5 views (last 30 days)
Robin Hellmers
Robin Hellmers on 3 Nov 2019
Commented: Walter Roberson on 6 Nov 2019
I have searched for several questions which partly does this, but I don't manage to do it all.

  1 Comment

Walter Roberson
Walter Roberson on 6 Nov 2019
Note: it is not completely possible to copy figures into subplots. figure objects can have interactive behaviour that cannot be associated with uipanels or axes.

Sign in to comment.

Answers (1)

Subhadeep Koley
Subhadeep Koley on 6 Nov 2019
Robin, refer to the below demo code which loads 2 figures from .fig files and plot them into a new subplot along with legends and axis labels.
close all;
% Create and save demo figures
fig1 = figure; plot(rand(1,50)); axis tight; legend('Data 1');
savefig(fig1,'fig1.fig');
fig2 = figure; plot(rand(1,100)); axis tight; legend('Data 2');
savefig(fig2,'fig2.fig');
% Close the figures
close all;
% Open fig1 and extract X, Y, legend values from it
fig = openfig('fig1.fig','invisible');
dataObjs = findobj(fig,'-property','YData');
y1 = dataObjs(1).YData;
dataObjs = findobj(fig,'-property','XData');
x1 = dataObjs(1).XData;
leg1 = findobj(fig,'type','legend');
legText1 = leg1.String{1};
% Open fig2 and extract X, Y, legend values from it
fig = openfig('fig2.fig','invisible');
dataObjs = findobj(fig,'-property','YData');
y2 = dataObjs(1).YData;
dataObjs = findobj(fig,'-property','XData');
x2 = dataObjs(1).XData;
leg2 = findobj(fig,'type','legend');
legText2 = leg2.String{1};
% Ploting them in a new subplot
figure;subplot(2,1,1);
plot(x1,y1); axis tight;
legend(legText1);
title('Copied from fig1');
subplot(2,1,2);
plot(x2,y2); axis tight;
legend(legText2);
title('Copied from fig2');
Hope this helps!

  0 Comments

Sign in to comment.

Sign in to answer this question.

Tags