MATLAB: How to find the size of a file on disk from MATLAB

MATLAB

I would like to find the size of a file on disk from MATLAB.

Best Answer

There are two ways to have your program determine the size of a file:
%-- METHOD #1 --
s = dir('myfile.dat');
filesize = s.bytes
%-- METHOD #2 --
fid = fopen('myfile.dat');
fseek(fid, 0, 'eof');
filesize = ftell(fid)
fclose(fid);
The dir function also returns the filename (s.name), last modification date (s.date), and whether or not it is a directory (s.isdir). (The second method requires read access to the file.)