Copy legends while superimposing two .fig files
조회 수: 5 (최근 30일)
이전 댓글 표시
I would like to copy the legends from the original figure f1 & f2 to the newly superimposed image.
I'm trying the below code but it does not code correctly.
GM_req =[2 8 9 19 22 32 34 38];
for i=1:length(GM_req)
% Load figure from Upper bound
figname=strcat('.\SMRF-50y-C4-Col-PZ-Beam-stress-UB\UB_GM_',num2str(GM_req(i)),'.fig');
f1=openfig(figname,'invisible');
hold on
% Load figure from Lower bound
figname=strcat('.\SMRF-50y-C4-Col-PZ-Beam-stress-LB\LB_GM_',num2str(GM_req(i)),'.fig');
f2=openfig(figname,'invisible');
figure
ax1 = gca;
copyobj(allchild(get(f1, 'CurrentAxes')), ax1);
ax2 = axes('Position',get(ax1,'Position'));
copyobj(allchild(get(f2, 'CurrentAxes')), ax2);
set(ax2,'Color','none','XTick',[],'YTick',[]);
lb_fig = findall(f2,'Type','Line'); % get current figure handle
ub_fig = findall(f1,'Type','Line');; % find upper bound figure handle
lb_legend = legend(ax1, [ub_fig(1), lb_fig(1)]);
close(f1)
close(f2)
hold off
end
댓글 수: 0
답변 (1개)
Rasmita
2023년 4월 12일
Hi Devang,
It is my understanding that, you are creating a new figure from each pair of upper and lower bound figures and trying to copy the legends from the original figures to the newly created superimposed figures.
For this you need to extract the legend object from the original figures and add it to the new figure. Here is a modified version of your code that should work:
GM_req = [2 8 9 19 22 32 34 38];
for i = 1:length(GM_req)
% Load figure from Upper bound
figname = strcat('.\SMRF-50y-C4-Col-PZ-Beam-stress-UB\UB_GM_',num2str(GM_req(i)),'.fig');
f1 = openfig(figname, 'invisible');
hold on
% Load figure from Lower bound
figname = strcat('.\SMRF-50y-C4-Col-PZ-Beam-stress-LB\LB_GM_',num2str(GM_req(i)),'.fig');
f2 = openfig(figname, 'invisible');
% Copy plot objects to new figure
figure
ax1 = gca;
copyobj(allchild(get(f1, 'CurrentAxes')), ax1);
ax2 = axes('Position', get(ax1, 'Position'));
copyobj(allchild(get(f2, 'CurrentAxes')), ax2);
set(ax2, 'Color', 'none', 'XTick', [], 'YTick', []);
% Copy legend objects to new figure
lb_legend = findobj(f2, 'Type', 'Legend');
ub_legend = findobj(f1, 'Type', 'Legend');
new_legend = copyobj([lb_legend, ub_legend], gcf);
set(new_legend, 'Location', 'best');
% Close original figures
close(f1)
close(f2)
hold off
end
This code finds the legend objects in the original figures using the ‘findobj’ function, copies them to the new figure using the ‘copyobj’ function, and sets the location of the new legend to "best" using the ‘set’ function.
For more information, please refer below documentations:
Hope this helps!
Regards,
Rasmita
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!