How can I loop through multiple Excel files while performing math operations on each file?

6 views (last 30 days)
Hello! I am trying to load in about 600 daily Excel files and count if the data in these files satisfy certain atmospheric conditions. The ultimate goal is to have a count of the number of days that satisfy the requirements.
This is the code that I wish to have done to each day written as though it was for one day:
clear all
close all
% read in file
dec0105 = xlsread('20051201.xlsx');
% assign variable names
heightm = dec0105(3:end,2);
heightkm = heightm/1000;
height1 = heightkm(1);
n = length(heightkm);
temp = dec0105(3:end,3);
% the definition of a cold pool (lowest 1.5 km of atmos with 65%
% 2.5 degC/km lapse rate or less)
hcutoff = heightkm(1)+1.5;
r = length(find(heightkm >= hcutoff));
rcutoff = n - r; % row index of last row with sufficient conditions
% calculate matrix of lapse rates
lamda = [];
for n = 1:rcutoff
lamdai = (-1 * (temp(n+1)-temp(n))/(heightkm(n+1)-heightkm(n)));
lamda = [lamda; lamdai];
end
% find percentage of lapse rates <= 2.5 degC/km
alllamda = length(lamda);
sufflamda = length(find(lamda <= 2.5));
perlamda = sufflamda/alllamda;
% 1 = cold pool event, 0 = no cold pool
cpcount = 0;
if perlamda >= 0.65
cpcount = cpcount + 1;
else
cpcount = cpcount;
end
This works as I want it to, I just want the added capability of looping over multiple days.

Answers (1)

dpb
dpb on 2 Apr 2015
I'm particularly fond of the dir solution which for your case would look something like
d=dir('*.xlsx');
for i=1:length(d)
data=xlsread(d(i).name);
...
insertyourscript(data)
end
  2 Comments
dpb
dpb on 11 Jan 2018
Oh, I doubt "nothing", just not the result expected, maybe! :)
Not enough information to go on; the structure of the worksheet is critical; the OP of this question had a numeric array such that default arguments returned the desired data; does that work for your case for an individual file? If not, you need to see what need to do to be able to get the desired data from an individual sheet first. If that works, then need to have the desired files on the working path to be found. What was the content of the dir structure d returned?
BTW, since the time of this posting, readtable has been introduced and can make much with xlsread now obsolete or outdated way to approach the problem.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!