Keep Headers in Loop and Skip Errors
Show older comments
Hi. I have two issues with my loop, and I'd appreciate any help.
First I am creating excel files for impOOB results. However, I just get the values. I would like to keep the Variable Names or Header Names.
Second, some of the spreadsheets in the loops do not have all the predictors I am specifying: 'Manhole','Catch','BackUp','PRCP', 'Street'
Matlab then stops running the loop. How can I get Matlab to skip over the errors and just ignore the spreadsheets?
Finally, this is just an extra question. I really rather have all the results in one excel file with a row for each spreadsheet name and the results in columns. Is there a way I can do this?
I've included three sample files that were in my directory. (My directory actually has 174 such files). Thanks again.
indir = 'C:\Users\Desktop\FilesforRF'; %path to input directory
outdir = 'C:\Users\Desktop\FilesforRF\results';
outdir2 = 'C:\Users\Desktop\FilesforRF\results2';
dinfo = dir(fullfile(indir, '*.xlsx'));
filenames = {dinfo.name};
nfiles = length(filenames);
for K = 1 : nfiles
thisfile = filenames{K};
infile = fullfile(indir, thisfile);
outfile = fullfile(outdir, thisfile);
outfile2 = fullfile(outdir2, thisfile);
p = readtable(infile,'PreserveVariableNames',true)
dsa = p;
X=dsa(:,ismember(dsa.Properties.VariableNames, {'Manhole','Catch','BackUp','PRCP'}))
Y=dsa(:,ismember(dsa.Properties.VariableNames, {'Street'}))
t = templateTree('NumVariablesToSample','all',...
'PredictorSelection','interaction-curvature','Surrogate','on');
rng(1); % For reproducibility
Mdl = fitrensemble(X,Y,'Method','Bag','NumLearningCycles',200, ...
'Learners',t);
yHat = oobPredict(Mdl);
R2 = corr(Mdl.Y,yHat)^2
impOOB = oobPermutedPredictorImportance(Mdl);
outdata = impOOB;
outdata2 = R2
writematrix(outdata, outfile); %needs R2019b or later %the sub-table!
writematrix(outdata2, outfile2);
end
Accepted Answer
More Answers (0)
Categories
Find more on Standard File Formats 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!