필터 지우기
필터 지우기

How do I making a legend in a for loop with blank spaces.

조회 수: 4 (최근 30일)
Jacob Lapping
Jacob Lapping 2015년 4월 28일
답변: Star Strider 2015년 4월 28일
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!

답변 (1개)

Star Strider
Star Strider 2015년 4월 28일
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.

카테고리

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