How can I get all the legends using this script?

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 Kommentare

Original question posted by SanSun retrieved from Google cache:
How can I get all the legends using this script?
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.
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.
(Answers Dev) Restored edit

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Alan Stevens
Alan Stevens am 19 Aug. 2020

0 Stimmen

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 Kommentare

You have legend(legText) inside the loop. It needs to be outside, after the end statement.
Ah, yes, I forgot. You need to ensure they are the same length, by padding them out with blanks where required.
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 am 21 Sep. 2020
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}];

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Graphics Object Properties finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 19 Aug. 2020

Kommentiert:

am 8 Okt. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by