How do I correctly store the output of a double for loop?

조회 수: 1 (최근 30일)
JJH
JJH 2018년 11월 20일
댓글: Cris LaPierre 2018년 11월 20일
I have a for loop that loops over each folder in a directory and then does another for loop over every file in that folder. I need to extract a parameter from each of these files and save every output into an array. I currently have something with the structure
Index = 1:13;
SpectrumFileInfo = dir('Spectrum Data*'); %Find and index the filenames
SpectrumFileInfo=SpectrumFileInfo(Index);
NSpec = length(SpectrumFileInfo);
peakposarray = zeros(26,N);
for k = 1:NSpec %NSpec is the number of folders with the given filename in the directory
FolderName = SpectrumFileInfo(k).name;
Direc = dir([FolderName '\I*.csv']); % directs the computer to the files within each folder
[~,idx] = sort([Direc.datenum]);
Direc = Direc(idx);
DataPointsSpec = length(Direc)-2;
for kk = 2:DataPointsSpec+1
SpectrumData = SpecRead([FolderName '\' Direc(kk).name]); % All this function does that is relevant here is csvread on each file
Wavelength=SpectrumData(2:end-1,1);
Intensity=SpectrumData(2:end-1,2);
[pks locs w]=findpeaks(Intensity,'NPeaks',8,'SortStr','descend','MinPeakDistance',10,'MinPeakProminence',1*10^-6,'MinPeakWidth',2); %built in function with some added restrictions
pw = (w*0.1); % some operations on the data
vec = [lambda,pw];
vecsort = sortrows(vec,1);
peakpos = 0.5*(sum(vecsort(end,:))-sum(vecsort(1,:)))+sum(vecsort(1,:));
for jj = 1:length(peakpos)
peakposarray(kk,jj) = peakpos(jj); % peakpos is the quantity I want to extract
end
end
end
This is close to doing what I want, except that I want to fill peakposarray such that each column is the set of values of peakpos for each folder. Each file has a single value of peakpos and each folder has 26 files. Currently, peakpos is being calculated for each file in each folder, but each time the values of peakpos are entered into peakposarray for a particular folder they replace the existing column of values (always the first column). I can't work out how to get the values from the first folder to be shown in the first column, the values from the second folder to be shown in the second column, etc. What is the problem with my for loop?
  댓글 수: 1
KALYAN ACHARJYA
KALYAN ACHARJYA 2018년 11월 20일
Can you elaborate the question with small simmilar example?

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

채택된 답변

Cris LaPierre
Cris LaPierre 2018년 11월 20일
Assuming there are NSpec folders with 26 files in each one. Also assuming there is one peakpos value per file.
...
peakposarray = zeros(26,NSpec);
for k = 1:NSpec %NSpec is the number of folders with the given filename in the directory
... % code to select files with folder k
for kk = 2:DataPointsSpec+1 % I don't understand your indexing, but assume it is correct)
... % code to load and process file kk.
peakposarray(kk-1,k) = 0.5*(sum(vecsort(end,:))-sum(vecsort(1,:)))+sum(vecsort(1,:)); % peakpos is the quantity I want to extract
end
end
Files correspond to rows (index kk) while folders correspond to columns (index k) in peakposarray. Note that when making the assignment, I use kk-1 because the for loop starts with kk=2.
  댓글 수: 1
Cris LaPierre
Cris LaPierre 2018년 11월 20일
To answer your final question, your problem is that your column index, jj, does not correspond to your folder loop counter, k. It also should always be 1 since peakpos is a single value for each file kk, meaning your column index never changes and you end up overwriting the previous values.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Debugging and Analysis에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by