check if you can load a matfile in matlab

You can use the matfile function in Matlab to check if a .mat file can be loaded.

The matfile function returns a matlab.io.MatFile object that provides access to the variables in the .mat file.

Here's an example:

main.m
try
    matObj = matfile('filename.mat');
    varNames = who(matObj); % return the variable names
catch ME
    errorMessage = ME.message;
end
142 chars
7 lines

If the file can be loaded, matObj will be the matlab.io.MatFile object and varNames will be a cell array of the variable names in the file.

If the file can't be loaded, execution will go to the catch block and ME.message will be the error message.

Note that matfile only checks if the file can be loaded, it doesn't actually load the file into memory. If you need to load the variables in the file into memory, you can use the load function.

related categories

gistlibby LogSnag