How to run calculation based on excel files in a folder path via for loop
Show older comments
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 Comments
dpb
on 22 Jun 2022
"I'm stuck at ... having matlab ignore any temporary files that starts with ~$"
filePattern = fullfile(myFolder, '*Day*.xlsx');
Use a matching wildcard to select a subset of files...but there shouldn't be open files matching those you're looking for unless you've got them open already in another Excel session or have crashed Excel using ActiveX and they're hanging around from unterminated Excel sessions. If you have been using ActiveX, you probably should check with the task manager how many sessions of Excel are still open -- it's easy if make mistake there to end up with a bunch of zombie processes you either must kill w/ task manager or, if you're not comfortable there, restarting the machine.
In that case, before you open any new Excel sessions, you can simply delete all the left over temporary files.
xlsread has been deprecated; I'd suggest depending on the file content readtable or readmatrix instead...
Emily
on 22 Jun 2022
Edited: Walter Roberson
on 22 Jun 2022
Walter Roberson
on 22 Jun 2022
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
Answers (0)
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!