Keep Headers in Loop and Skip Errors

2 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

CMatlabWold
CMatlabWold il 3 Set 2021
I figured out the first two. For the last, I believe I will use an excel merge type of function
I added Mdl.PredictorNames. And for my error issues, I used try catch.
outtable = array2table(outdata,'VariableNames',Mdl.PredictorNames)
for K = 1 : nfiles
try
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);
impOOB(impOOB<0) = 0;
impOOB = impOOB./sum(impOOB)
outdata = impOOB;
outdata2 = R2;
outtable = array2table(outdata,'VariableNames',Mdl.PredictorNames)
outtable2 = array2table(outdata2)
writetable(outtable, outfile); %needs R2019b or later %the sub-table!
writetable(outtable2, outfile2);
catch
fprintf('Inconsistent data in iteration %s, skipped.\n', K);
end
end
Unrecognized function or variable 'nfiles'.

Più risposte (0)

Categorie

Scopri di più su Just for fun in Help Center e File Exchange

Prodotti


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by