Try catch to load files

24 views (last 30 days)
Hi!! I'm writing a program in which I have to load multiple files within a cycle and work with them.
The program calls the different files each time it runs the cycle but I would luke to know what is the best way to handle an error when the file it is trying to load doesn't exist so it just continues to the next one, eliminating the need to stop the program run.
I thought of doing this with a try catch but couldn't find exactly how in the documentation

Accepted Answer

Walter Roberson
Walter Roberson on 18 Jul 2021
Edited: Walter Roberson on 18 Jul 2021
cminfo = which('cameraman.tif');
cmdir = fileparts(cminfo)
cmdir = '/MATLAB/toolbox/images/imdata'
dinfo = dir(cmdir);
dinfo([dinfo.isdir]) = [];
filenames = fullfile({dinfo.folder}, {dinfo.name});
nfiles = length(filenames);
results = cell(nfiles, 1);
for K = 1 : nfiles
thisfile = filenames{K};
try
%demonstration processing
fileinfo = imfinfo(thisfile);
results{K} = fileinfo;
catch ME
fprintf('not image file: "%s"\n', thisfile);
continue
end
end
not image file: "/MATLAB/toolbox/images/imdata/CT-MONO2-16-ankle.dcm" not image file: "/MATLAB/toolbox/images/imdata/Contents.m" not image file: "/MATLAB/toolbox/images/imdata/DICOMDIR" not image file: "/MATLAB/toolbox/images/imdata/US-PAL-8-10x-echo.dcm" not image file: "/MATLAB/toolbox/images/imdata/brain.nii" not image file: "/MATLAB/toolbox/images/imdata/brainMRI.hdr" not image file: "/MATLAB/toolbox/images/imdata/brainMRI.img" not image file: "/MATLAB/toolbox/images/imdata/buildingPixelLabeled.mat" not image file: "/MATLAB/toolbox/images/imdata/cellsequence.mat" not image file: "/MATLAB/toolbox/images/imdata/contours.mat" not image file: "/MATLAB/toolbox/images/imdata/defaultBRISQUEModel.mat" not image file: "/MATLAB/toolbox/images/imdata/defaultDnCNN-B-Grayscale.mat" not image file: "/MATLAB/toolbox/images/imdata/defaultNIQEModel.mat" not image file: "/MATLAB/toolbox/images/imdata/eSFRdefaultColorReference.mat" not image file: "/MATLAB/toolbox/images/imdata/eSFRdefaultGrayReference.mat" not image file: "/MATLAB/toolbox/images/imdata/imdemos.mat" not image file: "/MATLAB/toolbox/images/imdata/knee1.dcm" not image file: "/MATLAB/toolbox/images/imdata/knee2.dcm" not image file: "/MATLAB/toolbox/images/imdata/lab8.icm" not image file: "/MATLAB/toolbox/images/imdata/littlecoriver.lan" not image file: "/MATLAB/toolbox/images/imdata/mississippi.lan" not image file: "/MATLAB/toolbox/images/imdata/monitor.icm" not image file: "/MATLAB/toolbox/images/imdata/montana.lan" not image file: "/MATLAB/toolbox/images/imdata/mristack.mat" not image file: "/MATLAB/toolbox/images/imdata/niftiHeaderExample.hdr.gz" not image file: "/MATLAB/toolbox/images/imdata/office.hdr" not image file: "/MATLAB/toolbox/images/imdata/paris.lan" not image file: "/MATLAB/toolbox/images/imdata/pendulum.mat" not image file: "/MATLAB/toolbox/images/imdata/peppers.dpx" not image file: "/MATLAB/toolbox/images/imdata/regioncoordinates.mat" not image file: "/MATLAB/toolbox/images/imdata/rhinos.avi" not image file: "/MATLAB/toolbox/images/imdata/rio.lan" not image file: "/MATLAB/toolbox/images/imdata/rirePatient007CT.bin" not image file: "/MATLAB/toolbox/images/imdata/rirePatient007CT.header" not image file: "/MATLAB/toolbox/images/imdata/rirePatient007MRT1.bin" not image file: "/MATLAB/toolbox/images/imdata/rirePatient007MRT1.header" not image file: "/MATLAB/toolbox/images/imdata/rtstruct.dcm" not image file: "/MATLAB/toolbox/images/imdata/spiralVol.mat" not image file: "/MATLAB/toolbox/images/imdata/tokyo.lan" not image file: "/MATLAB/toolbox/images/imdata/traffic.avi" not image file: "/MATLAB/toolbox/images/imdata/traffic.mj2" not image file: "/MATLAB/toolbox/images/imdata/trees.mat" not image file: "/MATLAB/toolbox/images/imdata/westconcordpoints.mat"
results
results = 153×1 cell array
{1×1 struct} {1×1 struct} {1×1 struct} {1×1 struct} {1×1 struct} {1×1 struct} {1×1 struct} {1×1 struct} {1×1 struct} {1×1 struct} {0×0 double} {0×0 double} {0×0 double} {1×1 struct} {0×0 double} {1×1 struct}

More Answers (1)

Image Analyst
Image Analyst on 18 Jul 2021
The best way is to follow the FAQ:
You create a loop where you either only have files that exist (if you use the dir() function), or you check if the file exists with exist() or isfile(), and then you'll only process the files that do exist so no error ever gets thrown and there is no need to put
try
% Try to open file with whatever function you want.
catch ME
% Error opening the file, like it doesn't exist or you don't have permission.
continue; % Skip to bottom of for loop but continue interating.
end
but again, the above code is not recommended and I recommend you do it like in the FAQ.

Categories

Find more on Startup and Shutdown 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!