Clear Filters
Clear Filters

Merging 2 plots that are already saved as .fig files

1 view (last 30 days)
I have a script that reads data from an s2p file with data = read(rddata.data,name) where 'name is the name of the s2p file. I am then setting certain parameters for the plot like axis, legend, titles, and save the file as .fig file which i can open on its own. Is there a way to take 2 such files and put them on the same graph easily with just the info from the .fig file directly? I tried doing the following:
% Load saved .fig files
fig1 = openfig('File1.fig');
fig2 = openfig('File2.fig');
% Create new figure with tiled layout
figure(100);
tiledlayout(1,2);
%title('Figure 1');
% Plot axes contents from figure 1
nexttile;
ax1 = copyobj(fig1.Children, gcf);
%copyobj(axes1, gca);
% Plot axes contents from figure 2
nexttile;
ax2 = copyobj(fig2.Children, gcf);
saveas(gcf, 'combined_plot.png');
when I run this I get a weird overlay of just one of the plots and behind the one plot there seems to be 2 blank graph windows that are being blocked by the one graph showing.

Accepted Answer

Voss
Voss on 29 Apr 2024
Something like this might work, assuming each figure has one axes:
fig1 = openfig('File1.fig','invisible');
fig2 = openfig('File2.fig','invisible');
f = figure();
tl = tiledlayout(f,1,2);
ax = copyobj([fig1.CurrentAxes, fig1.CurrentAxes.Legend],tl);
ax(1).Layout.Tile = 1;
ax = copyobj([fig2.CurrentAxes, fig2.CurrentAxes.Legend],tl);
ax(1).Layout.Tile = 2;
delete([fig1 fig2]) % delete the invisible figures
  3 Comments
Walter Roberson
Walter Roberson on 30 Apr 2024
% Load saved .fig files
fig1 = openfig('file1.fig', 'invisible');
fig2 = openfig('file2.fig', 'invisible');
%f = figure();
figure();
tl = tiledlayout(1,2);
ax = copyobj([fig1.CurrentAxes, fig1.CurrentAxes.Legend],tl);
title(ax(1), 'New Title 1' , 'FontSize', 30);
ax(1).Layout.Tile = 1;
nexttile;
ax = copyobj([fig2.CurrentAxes, fig2.CurrentAxes.Legend],tl);
ax(1).Layout.Tile = 2;
title(ax(1), 'New Title 2' , 'FontSize', 30);
delete([fig1 fig2]) % delete the invisible figures
disp('done');
Voss
Voss on 30 Apr 2024
You're welcome!
As Walter points out, you need to specify the axes in the title call.
The overlay with the x and y axis tick labels is because of your use of nexttile. Notice it's not in my answer; remove it.
As for the legends not carrying over, they do with the fig files I'm using here. I guess I would need to have your actual fig files to see what the problem is. Please upload them using the paperclip button.
% Load saved .fig files
fig1 = openfig('File1.fig', 'invisible');
fig2 = openfig('File2.fig', 'invisible');
figure();
tl = tiledlayout(1,2);
ax = copyobj([fig1.CurrentAxes, fig1.CurrentAxes.Legend],tl);
title(ax(1),'New Title 1' , 'FontSize', 30);
ax(1).Layout.Tile = 1;
ax = copyobj([fig2.CurrentAxes, fig2.CurrentAxes.Legend],tl);
ax(1).Layout.Tile = 2;
title(ax(1),'New Title 2' , 'FontSize', 30);
delete([fig1 fig2]) % delete the invisible figures
disp('done');
done

Sign in to comment.

More Answers (0)

Categories

Find more on Specifying Target for Graphics Output 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!