tags:

views:

3763

answers:

3

What is the best way to figure out the size of a file using MATLAB? The first thought that comes to mind is size(fread(fid)).

A: 

Use the fact that MatLab has access to Java Objects:

myFile = java.io.File('filename_here')
flen = length(myFile)
KitsuneYMG
+10  A: 

You can use the DIR function to get directory information, which includes the sizes of the files in that directory. For example:

dirInfo = dir(dirName);  % Where dirName is the directory name where the
                         %   file is located
index = strcmp({dirInfo.name},fileName);  % Where fileName is the name of
                                          %   the file.
fileSize = dirInfo(index).bytes;  % The size of the file, in bytes

Or, since you are looking for only one file, you can do what Elazar said and just pass an absolute or relative path to your file to DIR:

fileInfo = dir('I:\kpe\matlab\temp.m');
fileSize = fileInfo.bytes;
gnovice
Hey, you earned a badge because of my answer (8>3*2). ;-)
Elazar Leibovich
Unfortunately, no. Your answer would have to have at least 10 upvotes, which means mine would have to have 20. I don't think there are enough people on SO who are interested in MATLAB for those sorts of numbers to happen. We can dream though... =)
gnovice
+9  A: 

Please see the dir function as stated above.

Please note that the dir function works on files and not on directories only.

>> s = dir('c:\try.c')

s = 

       name: 'try.c'
       date: '01-Feb-2008 10:45:43'
      bytes: 20
      isdir: 0
    datenum: 7.3344e+005
Elazar Leibovich
+1: Good point. I forgot to mention that you can also pass file names.
gnovice