代码之家  ›  专栏  ›  技术社区  ›  weiyin

如何在matlab中获得文件的大小?

  •  34
  • weiyin  · 技术社区  · 16 年前

    使用matlab计算文件大小的最佳方法是什么?首先想到的是 size(fread(fid)) .

    7 回复  |  直到 10 年前
        1
  •  55
  •   gnovice    13 年前

    请看 dir 功能如上所述。

    请注意,dir函数只在文件上工作,而不在目录上工作。

    >> s = dir('c:\try.c')
    
    s = 
    
           name: 'try.c'
           date: '01-Feb-2008 10:45:43'
          bytes: 20
          isdir: 0
        datenum: 7.3344e+005
    
        2
  •  22
  •   Community CDub    8 年前

    你可以使用 DIR 函数获取目录信息,其中包括该目录中文件的大小。例如:

    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
    

    或者,由于您只查找一个文件,因此可以执行以下操作 Elazar 只需将绝对或相对路径传递给dir:

    fileInfo = dir('I:\kpe\matlab\temp.m');
    fileSize = fileInfo.bytes;
    
        3
  •  7
  •   Ether    15 年前

    使用Matlab可以访问Java对象的事实:

    myFile = java.io.File('filename_here')
    flen = length(myFile)
    
        4
  •  5
  •   RamMan4x4    13 年前

    如果不想在目录中硬编码,可以使用内置的pwd工具查找当前目录,然后将文件名添加到其中。见下例:

    FileInfo = dir([pwd,'\tempfile.dat'])
    FileSize = FileInfo.bytes
    
        5
  •  2
  •   Shai    10 年前

    这个问题似乎表明 fopen / fread …使用。在这种情况下,为什么不寻找文件的结尾并读取位置呢?

    例子:

    function file_length = get_file_length(fid)
    % extracts file length in bytes from a file opened by fopen
    % fid is file handle returned from fopen
    
    % store current seek
    current_seek = ftell(fid);
    % move to end
    fseek(fid, 0, 1);
    % read end position
    file_length = ftell(fid);
    % move to previous position
    fseek(fid, current_seek, -1);
    
    end
    

    Matlab可以提供一个快捷方式。

    更多关于 ftell 可以找到 here .

        6
  •  1
  •   Shan    12 年前

    此代码适用于任何文件和目录(不需要绝对路径):

        dirInfo=dir(pwd);
        index = strcmp({dirInfo.name},[filename, '.ext']); % change the ext to proper extension 
        fileSize = dirInfo(index).bytes
    
        7
  •  -1
  •   Faizullah    10 年前

    查找文件大小的简单方法是: 输入这些命令

    k=imfinfo('filename.formate');

    size_of_file=k.file size

    获取文件大小。

    推荐文章