How do you get the size of a file in MATLAB?

前端 未结 7 1827
耶瑟儿~
耶瑟儿~ 2020-12-09 07:36

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)).

相关标签:
7条回答
  • 2020-12-09 08:10

    If you don't want to hardcode in your directory, you can use the built in pwd tool to find the current directory and then add your file name to it. See example below:

    FileInfo = dir([pwd,'\tempfile.dat'])
    FileSize = FileInfo.bytes
    
    0 讨论(0)
  • 2020-12-09 08:13

    Easy way to find size of file is: enter these cammands

    K=imfinfo('filename.formate');

    size_of_file=K.FileSize

    and get size of file.

    0 讨论(0)
  • 2020-12-09 08:14

    This code works for any file and directory (no need for absolute path) :

        dirInfo=dir(pwd);
        index = strcmp({dirInfo.name},[filename, '.ext']); % change the ext to proper extension 
        fileSize = dirInfo(index).bytes
    
    0 讨论(0)
  • 2020-12-09 08:27

    Use the fact that MatLab has access to Java Objects:

    myFile = java.io.File('filename_here')
    flen = length(myFile)
    
    0 讨论(0)
  • 2020-12-09 08:28

    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
    
    0 讨论(0)
  • 2020-12-09 08:29

    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;
    
    0 讨论(0)
提交回复
热议问题