check if matfile exists in matlab

You can check if a "filename.mat" file exists in Matlab using the exist function. The code to perform this would look like this:

main.m
if exist('filename.mat', 'file')
    disp('File exists!');
else
    disp('File does not exist!');
end
102 chars
6 lines

The exist function returns 2 if the file exists, 0 if it does not, and 7 if it exists on the Matlab search path, but is not found in the current directory. The second argument, 'file', is passed to tell exist to search for a file with that name specifically.

related categories

gistlibby LogSnag