MATLAB: Reading mat or ascii files

asciiloadmatMATLAB

Hello. I have a strange problem. I am reading two types of files '-ascii' and '-mat' but the load function must be set to one of them but I don't know what tpye of file is currently reading. My question is how to write a code so it will be work for both types of files.

Best Answer

if exist(File, 'file') == 0
error('File does not exist: %s', File);
end
try
Data = load(File, '-MAT');
catch
Data = load(File, '-ASCII');
end