# Is it possible to identify data in a for loop?

조회 수: 1(최근 30일)
Feliciano Döring 2020년 8월 1일
댓글: Feliciano Döring 2020년 8월 2일
I have a code in which I pull files from '.txt' files and run them in a for loop in order to plot them all together. I was wondering if there was a way for me to identify each line separately in the plot. Here is the code and an example file,
d=dir(fullfile(myFolder, '*.txt'))
figure, hold on
for k = 1:numel(d)
plot(data(:,4),data(:,5),'LineWidth',3);
xlim([0 7])
ylim([0 4])
end

댓글을 달려면 로그인하십시오.

### 채택된 답변

jonas 2020년 8월 1일
I would probably collect the data in the loop instead and then call plot outside of the loop. Could use something like this:
d=dir(fullfile(myFolder, '*.txt'))
for k = 1:numel(d)
x{k} = data(:,4);
y{k} = data(:,5);
end
figure;hold on
h = cellfun(@plot,x,y,'uniformoutput',1);
xlim([0 7])
ylim([0 4])
legend(h,{d.name})
If you really want to label the data inside the loop then you can set the displayname:
d=dir(fullfile(myFolder, '*.txt'))
figure, hold on
for k = 1:numel(d)
h(k) = plot(data(:,4),data(:,5),'LineWidth',3,'displayname',d(k).name);
xlim([0 7])
ylim([0 4])
end
legend
##### 댓글 수: 6표시숨기기 이전 댓글 수: 5
Feliciano Döring 2020년 8월 2일
Thanks!

댓글을 달려면 로그인하십시오.

R2015a

### Community Treasure Hunt

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

Start Hunting!

Translated by