Merging 2 plots that are already saved as .fig files
조회 수: 2 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
채택된 답변
Voss
2024년 4월 29일
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
Walter Roberson
2024년 4월 30일
% 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
2024년 4월 30일
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');
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Specifying Target for Graphics Output에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!