检查文件MATLAB的大小

时间:2013-11-12 14:24:04

标签: matlab

我想查看文件的大小

如果我使用

是正确的
aux = dir(diary_file);
sizeOfFile = aux.bytes;

bytes: 362

现在我想检查文件大小是否> 1,我该怎么做

2 个答案:

答案 0 :(得分:3)

除非我在这里遗漏了一些东西,否则它非常简单:

if sizeOfFile > 1
   disp('Size of file is greater than 1'); % or do whatever else you want in that case
else
   disp('Size of file is less or equal to 1'); % or do whatever else you want in that case
end

答案 1 :(得分:3)

只是为了获得一些乐趣,这里有一个稍微有点傻逼的版本:

try 
    aux = dir(diary_file);
catch ME
    ME2 = MException('insert:id', 'Could not get directory listing for file/dir:');
    throw(addCause(ME2, ME));
end

if ~isempty(aux) 

    if numel(aux) == 1
        sz = aux.bytes;
    elseif aux.isdir
        error('insert:id', 'Expected single file; got directory listing.');
    else
        error('insert:id', 'Inconsistent directory listing.');
    end

    if ispc
        [~,~,ext] = fileparts(aux.name);
        if strcmpi(ext, '.lnk')
            warning('insert:id', ...
               'File seems to be a link; size may be misrepresented.'); 
        end
    end

    if sz > 1
        % CHECK PASSED
    else
        % CHECK NOT PASSED
    end

else
    error('insert:id', 'File does not exist.');
end