How do I making a legend in a for loop with blank spaces.
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I have 100+ files in the current directory. My code is intended to run a loop that goes through each of them and checks if they end in 'o1sCHANGED.txt'. If the file does end in that text, I want it to be plotted. The plot looks fine, the issue is with the legend. I create a variable called legendinfo{j} = q(j).name, which I ultimately want to write legend(legendinfo) to create a legend on the graph that corresponds to the plots, as suggested http://www.mathworks.com/matlabcentral/answers/29799-adding-a-legend-and-different-coloured-lines-in-a-for-loop
j=1;
q = dir('*.txt');
for j=1:numel(q)
if(q(j).name(end-13:end) == 'o1sCHANGED.txt' )
legendinfo{j} = q(j).name;
figure(1)
plot(FinalData{j}.BindingEnergy_eV_, FinalData{j}.Intensity_Counts_sec_);
hold on
end
legend(legendinfo)
The problem is legendinfo does not match up in size with the plot because not every file ends in o1sCHANGED.txt.
legendinfo is the size of numel(q) that only contains values where the index q matches up with a filename that ends in o1sCHANGED.txt'.
Double clicking on the workspace on the variable legendinfo gives
1 = []
2 = []
3 = 'filename_ending_in_o1sCHANGED.txt'
4 = []
5 = 'filename......
etc.
Please help!
0 comentarios
Respuestas (1)
Star Strider
el 28 de Abr. de 2015
I can’t run your code to test this, but if I understand your code correctly, I would set up a separate counter for ‘legendinfo’.
For example:
k1 = 0;
for j=1:numel(q)
if(q(j).name(end-13:end) == 'o1sCHANGED.txt' )
k1 = k1+1;
legendinfo{k1} = q(j).name;
That should at least eliminate the empty entries.
0 comentarios
Ver también
Categorías
Más información sobre Legend 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!