Running the Same code for Multiple Files in a Folder

Hi,
tt10001 = readtable('10001.xlsx');
dsa = tt10001;
modelspec = 'Street ~ Catch*BackUp*PRCP - Catch:BackUp:Break:Manhole:PRCP';
mdl10001 = fitglm(dsa,modelspec,'Distribution','poisson')
I have multiple excel files (almost 200), such as "10001.xlsx" in a folder. The spreadsheets are of various zip codes, such as "10002.xlsx", "10003.xlsx"...
How can I run this code for every file in the folder?
And have the results "mdl10001.CoefficientCovariance" of each file exported to its own Excel spreadsheet, as in xlswrite?
Thanks. I'd very much appreciate any help with this.

 採用された回答

Walter Roberson
Walter Roberson 2020 年 7 月 1 日

0 投票

indir = '.'; %path to input directory
outdir = 'results'; %path to where to put the results
if ~exist(outdir, 'dir'); mkdir(outdir); end
dinfo = dir(fullfile(indir, '*.xlsx'));
filenames =
nfiles = length(filenames);
for K = 1 : nfiles
thisfile = filenames{K};
infile = fullfile(indir, thisfile);
outfile = fullfile(outdir, thisfile);
tt10001 = readtable(infile);
dsa = tt10001;
modelspec = 'Street ~ Catch*BackUp*PRCP - Catch:BackUp:Break:Manhole:PRCP';
mdl10001 = fitglm(dsa, modelspec, 'Distribution', 'poisson');
outdata = mdl10001.CoefficientCovariance;
writematrix(outdata, outfile); %needs R2019b or later
end
If you are using earlier than R2019b then the last few lines would be
outdata = mdl10001(:,'CoefficientCovariance'); %the sub-table!
writetable(outdata, outfile);

6 件のコメント

CMatlabWold
CMatlabWold 2020 年 7 月 1 日
Thank you! What do I put for line5 filenames =
CMatlabWold
CMatlabWold 2020 年 7 月 1 日
I tried filenames = {dinfo(:).name}.'
But, its now working. Its only reading the first column of the worksheet, which is "BackUp"
And, the columns are the following:
BackUp Catch Manhole PRCP Street
I have Matlab 2018a, but
outdata = mdl10001(:,'CoefficientCovariance'); %the sub-table!
writetable(outdata, outfile);
gives me an error response:
Error in writetable (line 124)
write(a,filename,varargin{:})
Walter Roberson
Walter Roberson 2020 年 7 月 1 日
Sorry, I amn not sure what happened there,
filenames = {dinfo.name};
Can you attach a sample .xlsx for testing?
CMatlabWold
CMatlabWold 2020 年 7 月 1 日
編集済み: CMatlabWold 2020 年 7 月 1 日
Sure. Thanks. I've attached sample 4 spreadsheets. I am supposed to run the code for each zip code in NYC, individually. In my folder, I have 195 zip codes (195 spreadsheets).
Walter Roberson
Walter Roberson 2020 年 7 月 1 日
10004.xlsx fails because it does not contain a column for "Break"
CMatlabWold
CMatlabWold 2020 年 7 月 2 日
It works perfectly. Thank you!

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by