How can I get all the legends using this script?
Ältere Kommentare anzeigen
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
Rik
am 21 Sep. 2020
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.
John D'Errico
am 21 Sep. 2020
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
am 8 Okt. 2020
(Answers Dev) Restored edit
Antworten (1)
Alan Stevens
am 19 Aug. 2020
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
Alan Stevens
am 19 Aug. 2020
You have legend(legText) inside the loop. It needs to be outside, after the end statement.
Alan Stevens
am 19 Aug. 2020
Ah, yes, I forgot. You need to ensure they are the same length, by padding them out with blanks where required.
Rik
am 21 Sep. 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}];
Kategorien
Mehr zu Graphics Object Properties finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!