How to run calculation based on excel files in a folder path via for loop

조회 수: 1 (최근 30일)
Emily
Emily 2022년 6월 22일
댓글: Walter Roberson 2022년 6월 22일
I'm trying to create a GUI that will ask the user for a folder path and do some calculations based on it.
There's anywhere between 1-15 files per folder.
I'm stuck at printing the table output and having matlab ignore any temporary files that starts with ~$
filePattern = fullfile(myFolder, '*.xlsx');
theFiles = dir(filePattern);
for k=1: length(theFiles)
baseFileName= theFiles(k).name;
fullFileName=fullfile(myFolder, baseFileName);
Day{k}=extractBetween(fullFileName,"Day","_");
data1{k} =xlsread(theFiles(k), 2,'F10:end10');
TotalTemp{k}= sum(data1{k});
data2{k} =xlsread(theFiles(k), 3,'A10:end10');
TotalCost{k}= mean(data2{k});
T=table(Day, TotalTemp, TotalCost)
end
  댓글 수: 3
Emily
Emily 2022년 6월 22일
편집: Walter Roberson 2022년 6월 22일
I was able to update the code to skip the tempoary files.
filelist = dir;
filelist = filelist(~startsWith({filelist.name}, '~$''));
And have updated to readmatrix.
I'm still having trouble getting it to loop and print out the correct output.
for k=1: length(theFiles)
baseFileName= theFiles(k).name;
fullFileName=fullfile(myFolder, baseFileName);
day{k}=extractBetween(fullFileName,"Day","_");
data1{k} =readMatrix(theFiles(k),'Sheet', 'B', 'Range','F10:end10');
data1{k}=rmmissing(data1{k});
total1{k}= sum(data1{k});
data2{k} =readMatrix(theFiles(k),'Sheet', 'C', 'Range','A10:end10');
data2{k}=rmmissing(data2{k});
total2{k}= mean(data2{k});
total3{k}=total1{k}*total2{k}-20;
T=table(day, total1, total2, total3);
end
Walter Roberson
Walter Roberson 2022년 6월 22일
data1{k} =readMatrix(theFiles(k),'Sheet', 'B', 'Range','F10:end10');
theFiles(k) is the struct with the directory entry. You should be passing in the file name, which you stored in fullFileName

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by