How can I get all the legends using this script?

조회 수: 3 (최근 30일)
SanSun
SanSun 2020년 8월 19일
댓글: Rena Berman 2020년 10월 8일
Hi, I have multiple open figures. I'd like to create a new figure containing all the plots (the plots are output from different scripts). I 'd like to combine those plots to compare my results. I have attached a fig file in which I have combined 2 plots , and below is the code :
rep = 'C:\';
if exist(rep, 'file')~=7
error('Le dossier n''existe pas');
end
ext = '*.fig';
chemin = fullfile(rep,ext);
list = dir(chemin);
nFig = length(list);
figure; % new figure
for n = 1:nFig
filename = fullfile(rep,list(n).name);
f_c = openfig(filename,'reuse');
Fig = findobj(f_c,'Type','line');
x_data=get(Fig,'xdata');
y_data=get(Fig,'ydata');
leg = findobj(f_c,'type','legend');
legText = leg.String{1};
close(f_c);
hold on
p = plot(x_data,y_data,'color',rand(1,4),'linewidth',2); %plot the data again
hleg = legend(p,legText,'Location','best');
set(hleg,'fontsize',12);
title(' XXX ');
dockfig('all')
end
hold off;
the problem that only the legend of the last plot is copied. How can I get all the legends using this script?
Any help would be appreciated.
  댓글 수: 3
John D'Errico
John D'Errico 2020년 9월 21일
Note that when you remove your question, you damage answers as a site, since this makes the question useless for anyone else to ever learn from. This insults both the person who wasted their time in answering your question, as well as anyone who might otherwise have also gained from this answer.
If you cannot bear to leave your question in the public, then you should not post in the first place.
Rena Berman
Rena Berman 2020년 10월 8일
(Answers Dev) Restored edit

댓글을 달려면 로그인하십시오.

답변 (1개)

Alan Stevens
Alan Stevens 2020년 8월 19일
Try something like:
legText = [];
before your for loop. Then
legText = [legText; leg.String{1}];
within the loop, and
legend(legText)
after the loop ends (removing any attempt to set the legend within the loop).
  댓글 수: 4
Rik
Rik 2020년 9월 21일
Deleted comment by SanSun on 19 Aug 2020:
Thank you for your help but it doesn't work.
I tried that :
if exist(rep, 'file')~=7
error('Le dossier n''existe pas');
end
ext = '*.fig';
chemin = fullfile(rep,ext);
list = dir(chemin);
nFig = length(list);
figure; % new figure
legText = [];
for n = 1:nFig
filename = fullfile(rep,list(n).name);
f_c = openfig(filename,'reuse');
Fig = findobj(f_c,'Type','line');
x_data=get(Fig,'xdata');
y_data=get(Fig,'ydata');
leg = findobj(f_c,'type','legend');
legText = [legText; leg.String{1}];
close(f_c);
hold on
plot(x_data,y_data,'color',rand(1,4),'linewidth',2); %plot the data again
legend(legText)
title(' XXX ');
dockfig('all')
end
hold off;
I got this error :
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
Error in test_figure4 (line 22)
legText = [legText; leg.String{1}];
Rik
Rik 2020년 9월 21일
Deleted comment by SanSun on 19 Aug 2020:
I tried to put the legend outside the loop but it still doesn't work. I got this error :
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
Error in test_figure4 (line 22)
legText = [legText; leg.String{1}];

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by