unable to iterate serially through files in a for loop
I am running a matlab for loop to iterate through 30 .csv files and 30 .xlsx files. At each iteration of the for loop, i expect matlab to read the days from the .xlsx file, pass it to the .csv file so the same days can be selected and then do the calculation in the code. The code runs well, but the files saved are not showing the correct answers like when the calculations are done individually for each .mat and .xlsx file at a time. Only the anwers in the first saved for loop file is correct, the others are not what i expect. it looks like matlab is skipping the files or mixing them up in the for loop iteration, as only the first iteration is correct. The files are arranged serially in my working folder, hence i expect the for loop to iterate serially. Attached here are some of three of the files. Also attached here is the code. Please the main point of concern are the for loops which iterates through the .xlsx and .csv files. Thank you.
startIndex = 1;
endIndex = 3;
startIndex1 = 1;
endIndex1 = 3;
filelist = dir(‘C:UsersshedrDownloadstec data2017DOBUM2*.csv’);
filelistt = dir(‘C:UsersshedrDownloadstec data2017DOBUM2*.xlsx’);
output1 = cell(31,1);
for fileidx = startIndex:endIndex
% for fileidx = 1:numel(filelist)
spectrum = readmatrix(filelist(fileidx).name);
% y = spectrum(2:end,1:4);
% ctm = cell2mat(y)
e = length(spectrum);
[F] = fillmissing(spectrum, ‘linear’);
for fileidx1 = startIndex1:endIndex1
% for fileidx1 = 1:numel(filelistt)
if fileidx == fileidx1
spectrum1 = readmatrix(filelistt(fileidx1).name);
% % trying to extract the 5 quiet days
yy1 = spectrum1(:,1);
% ctm1 = cell2mat(yy1);
rmn1 = F(:,1) == yy1;
sbd1 = F(rmn1, :);
df1 = sbd1(:,4);
dd = size(df1);
yy2 = spectrum1(:,2);
% ctm2 = cell2mat(yy2);
rmn2 = F(:,1) == yy2;
sbd2 = F(rmn2, :);
df2 = sbd2(:,4);
dd = size(df2);
yy3 = spectrum1(:,3);
% ctm3 = cell2mat(yy3);
rmn3 = F(:,1) == yy3;
sbd3 = F(rmn3, :);
df3 = sbd3(:,4);
dd = size(df3);
yy4 = spectrum1(:,4);
% ctm4 = cell2mat(yy4);
rmn4 = F(:,1) == yy4;
sbd4 = F(rmn4, :);
df4 = sbd4(:,4);
dd = size(df4);
yy5 = spectrum1(:,5);
% ctm5 = cell2mat(yy5);
rmn5 = F(:,1) == yy5;
sbd5 = F(rmn5, :);
df5 = sbd5(:,4);
dd = size(df5);
% concatenating of the 5 quiet days
c = [df1,df2,df3,df4,df5];
% mean along horz. line
cm = mean(c,2);
% mean for the entire column
cmm = mean(cm);
crp = repmat(cmm,24,1);
cmm1 = cm-crp;
cmm2 = cmm1.^2;
cmm3 = mean(cmm2);
cmm4 = sqrt(cmm3); %% this is the standard deviation
mc = cm’;
ee = e/24;
con = repmat(mc,1,ee);
cno = con’;
tyc = F(:,4);
tycc = tyc-cno
ty = F(:,1);
% concatenating the time column with the computed tec values.
yt = [ty,tycc];
elseif fileidx <= 10
save([‘C:UsersshedrDownloadstec data2017DOBUM2casc’, num2str(fileidx)],’yt’)
% elseif fileidx <= 20
% save([‘C:UsersshedrDownloadstec data2017DOBUM2frns’, num2str(fileidx)],’yt’)
% elseif fileidx <= 30
% save([‘C:UsersshedrDownloadstec data2017DOBUM2func’, num2str(fileidx)],’yt’)
% we = size(cno)
end
end
% output1{fileidx} = y;
endI am running a matlab for loop to iterate through 30 .csv files and 30 .xlsx files. At each iteration of the for loop, i expect matlab to read the days from the .xlsx file, pass it to the .csv file so the same days can be selected and then do the calculation in the code. The code runs well, but the files saved are not showing the correct answers like when the calculations are done individually for each .mat and .xlsx file at a time. Only the anwers in the first saved for loop file is correct, the others are not what i expect. it looks like matlab is skipping the files or mixing them up in the for loop iteration, as only the first iteration is correct. The files are arranged serially in my working folder, hence i expect the for loop to iterate serially. Attached here are some of three of the files. Also attached here is the code. Please the main point of concern are the for loops which iterates through the .xlsx and .csv files. Thank you.
startIndex = 1;
endIndex = 3;
startIndex1 = 1;
endIndex1 = 3;
filelist = dir(‘C:UsersshedrDownloadstec data2017DOBUM2*.csv’);
filelistt = dir(‘C:UsersshedrDownloadstec data2017DOBUM2*.xlsx’);
output1 = cell(31,1);
for fileidx = startIndex:endIndex
% for fileidx = 1:numel(filelist)
spectrum = readmatrix(filelist(fileidx).name);
% y = spectrum(2:end,1:4);
% ctm = cell2mat(y)
e = length(spectrum);
[F] = fillmissing(spectrum, ‘linear’);
for fileidx1 = startIndex1:endIndex1
% for fileidx1 = 1:numel(filelistt)
if fileidx == fileidx1
spectrum1 = readmatrix(filelistt(fileidx1).name);
% % trying to extract the 5 quiet days
yy1 = spectrum1(:,1);
% ctm1 = cell2mat(yy1);
rmn1 = F(:,1) == yy1;
sbd1 = F(rmn1, :);
df1 = sbd1(:,4);
dd = size(df1);
yy2 = spectrum1(:,2);
% ctm2 = cell2mat(yy2);
rmn2 = F(:,1) == yy2;
sbd2 = F(rmn2, :);
df2 = sbd2(:,4);
dd = size(df2);
yy3 = spectrum1(:,3);
% ctm3 = cell2mat(yy3);
rmn3 = F(:,1) == yy3;
sbd3 = F(rmn3, :);
df3 = sbd3(:,4);
dd = size(df3);
yy4 = spectrum1(:,4);
% ctm4 = cell2mat(yy4);
rmn4 = F(:,1) == yy4;
sbd4 = F(rmn4, :);
df4 = sbd4(:,4);
dd = size(df4);
yy5 = spectrum1(:,5);
% ctm5 = cell2mat(yy5);
rmn5 = F(:,1) == yy5;
sbd5 = F(rmn5, :);
df5 = sbd5(:,4);
dd = size(df5);
% concatenating of the 5 quiet days
c = [df1,df2,df3,df4,df5];
% mean along horz. line
cm = mean(c,2);
% mean for the entire column
cmm = mean(cm);
crp = repmat(cmm,24,1);
cmm1 = cm-crp;
cmm2 = cmm1.^2;
cmm3 = mean(cmm2);
cmm4 = sqrt(cmm3); %% this is the standard deviation
mc = cm’;
ee = e/24;
con = repmat(mc,1,ee);
cno = con’;
tyc = F(:,4);
tycc = tyc-cno
ty = F(:,1);
% concatenating the time column with the computed tec values.
yt = [ty,tycc];
elseif fileidx <= 10
save([‘C:UsersshedrDownloadstec data2017DOBUM2casc’, num2str(fileidx)],’yt’)
% elseif fileidx <= 20
% save([‘C:UsersshedrDownloadstec data2017DOBUM2frns’, num2str(fileidx)],’yt’)
% elseif fileidx <= 30
% save([‘C:UsersshedrDownloadstec data2017DOBUM2func’, num2str(fileidx)],’yt’)
% we = size(cno)
end
end
% output1{fileidx} = y;
end I am running a matlab for loop to iterate through 30 .csv files and 30 .xlsx files. At each iteration of the for loop, i expect matlab to read the days from the .xlsx file, pass it to the .csv file so the same days can be selected and then do the calculation in the code. The code runs well, but the files saved are not showing the correct answers like when the calculations are done individually for each .mat and .xlsx file at a time. Only the anwers in the first saved for loop file is correct, the others are not what i expect. it looks like matlab is skipping the files or mixing them up in the for loop iteration, as only the first iteration is correct. The files are arranged serially in my working folder, hence i expect the for loop to iterate serially. Attached here are some of three of the files. Also attached here is the code. Please the main point of concern are the for loops which iterates through the .xlsx and .csv files. Thank you.
startIndex = 1;
endIndex = 3;
startIndex1 = 1;
endIndex1 = 3;
filelist = dir(‘C:UsersshedrDownloadstec data2017DOBUM2*.csv’);
filelistt = dir(‘C:UsersshedrDownloadstec data2017DOBUM2*.xlsx’);
output1 = cell(31,1);
for fileidx = startIndex:endIndex
% for fileidx = 1:numel(filelist)
spectrum = readmatrix(filelist(fileidx).name);
% y = spectrum(2:end,1:4);
% ctm = cell2mat(y)
e = length(spectrum);
[F] = fillmissing(spectrum, ‘linear’);
for fileidx1 = startIndex1:endIndex1
% for fileidx1 = 1:numel(filelistt)
if fileidx == fileidx1
spectrum1 = readmatrix(filelistt(fileidx1).name);
% % trying to extract the 5 quiet days
yy1 = spectrum1(:,1);
% ctm1 = cell2mat(yy1);
rmn1 = F(:,1) == yy1;
sbd1 = F(rmn1, :);
df1 = sbd1(:,4);
dd = size(df1);
yy2 = spectrum1(:,2);
% ctm2 = cell2mat(yy2);
rmn2 = F(:,1) == yy2;
sbd2 = F(rmn2, :);
df2 = sbd2(:,4);
dd = size(df2);
yy3 = spectrum1(:,3);
% ctm3 = cell2mat(yy3);
rmn3 = F(:,1) == yy3;
sbd3 = F(rmn3, :);
df3 = sbd3(:,4);
dd = size(df3);
yy4 = spectrum1(:,4);
% ctm4 = cell2mat(yy4);
rmn4 = F(:,1) == yy4;
sbd4 = F(rmn4, :);
df4 = sbd4(:,4);
dd = size(df4);
yy5 = spectrum1(:,5);
% ctm5 = cell2mat(yy5);
rmn5 = F(:,1) == yy5;
sbd5 = F(rmn5, :);
df5 = sbd5(:,4);
dd = size(df5);
% concatenating of the 5 quiet days
c = [df1,df2,df3,df4,df5];
% mean along horz. line
cm = mean(c,2);
% mean for the entire column
cmm = mean(cm);
crp = repmat(cmm,24,1);
cmm1 = cm-crp;
cmm2 = cmm1.^2;
cmm3 = mean(cmm2);
cmm4 = sqrt(cmm3); %% this is the standard deviation
mc = cm’;
ee = e/24;
con = repmat(mc,1,ee);
cno = con’;
tyc = F(:,4);
tycc = tyc-cno
ty = F(:,1);
% concatenating the time column with the computed tec values.
yt = [ty,tycc];
elseif fileidx <= 10
save([‘C:UsersshedrDownloadstec data2017DOBUM2casc’, num2str(fileidx)],’yt’)
% elseif fileidx <= 20
% save([‘C:UsersshedrDownloadstec data2017DOBUM2frns’, num2str(fileidx)],’yt’)
% elseif fileidx <= 30
% save([‘C:UsersshedrDownloadstec data2017DOBUM2func’, num2str(fileidx)],’yt’)
% we = size(cno)
end
end
% output1{fileidx} = y;
end for loop, iteration MATLAB Answers — New Questions