I have a script wherein I have two nested for loops. The first for loop goes into a series of five directories. The second for loop goes into files within the directories and performs a series of analyses on waveforms. I'm trying to store certain values each time the script makes a loop through one directory. There's something wrong with my syntax obviously because instead of creating a new column for each directory its just adding onto the column the new values for the subsequent directories. I'm having difficulties getting it to start a new column when it stores. Any help would be much appreciated! Here's an example of a value I'm wanting to store:
keep2=[]
for i=1:length(dirs)
for pp=1:length(files)
keep(:,pp)=(snr510(:,pp)>1)&(snr1015>1)&(snr1520)>1;
keep2=[keep2;keep(:,pp];
end
end
Again instead of storing the values each time I move through the loop into a new column it just adds onto keep2.

 채택된 답변

Jan
Jan 2011년 7월 26일

1 개 추천

Do the directories have the same number of files? Then a 3D-array would be fine:
keep = zeros(1, length(dirs), length(files)); % Pre-allocate!
for i=1:length(dirs)
for pp=1:length(files)
keep(:, i, pp) = (snr510(:,pp)>1) & (snr1015>1) & (snr1520)>1;
end
end
If the directories have a different number of files, you need a CELL:
keepC = cell(1, length(dirs));
for i=1:length(dirs)
keep = zeros(size(snr510, 1), length(files));
for pp=1:length(files)
keep(:, pp) = (snr510(:,pp)>1) & (snr1015>1) & (snr1520)>1;
end
keepC{i} = keep;
end

댓글 수: 1

Katherine Anderson
Katherine Anderson 2011년 7월 26일
Thanks Jan! The cell seemed to work..although I missed the {i} when I first did it and had quite the time messing around with things. Now I have the values stored in the cells individually for each directory-I'm gonna go ahead and store that way from now on..although its created a few other complications in my script that I'll have to iron out..I appreciate the help!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by