How can I get all the legends using this script?
    6 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
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 comentarios
  John D'Errico
      
      
 el 21 de Sept. de 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.
Respuestas (1)
  Alan Stevens
      
      
 el 19 de Ag. de 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 comentarios
  Rik
      
      
 el 21 de Sept. de 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
      
      
 el 21 de Sept. de 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}];
Ver también
Categorías
				Más información sobre Graphics Object Properties en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




