【问题标题】:MATLAB: Get the last modification time of a fileMATLAB:获取文件的最后修改时间
【发布时间】:2013-07-08 08:51:48
【问题描述】:

如果file.csv 的编辑时间比file.m 更新时间,我正在寻找执行一些例程的MATLAB 代码(更新file.m)。

应该是这样的:

% Write time extraction
tempC     = GetFileTime('file.csv', [], 'Write');
tempdateC = tempC.date
tempM     = GetFileTime('file.m', [], 'Write');
tempdateM = tempM.date

% Write time comparison
if numel(dir('file.m')) == 0 || tempdateC >= tempdateM
    matDef = regexprep(fileread('file.csv'), '(\r\n|\r|\n)', ';\n');
    f = fopen('file.m', 'w');
    fwrite(f, ['Variable = [' matDef(1:end) '];']);
    fclose(f);
end

时间戳提取的行似乎是不正确的 MATLAB 代码。其余的工作 (Evaluate variables in external file strings)。

【问题讨论】:

    标签: matlab timestamp


    【解决方案1】:

    您可以使用 MATLAB 的 dir 命令提取文件的修改时间。比如:

    function modTime = GetFileTime(fileName)
    listing = dir(fileName);
    % check we got a single entry corresponding to the file
    assert(numel(listing) == 1, 'No such file: %s', fileName);
    modTime = listing.datenum;
    end
    

    请注意,输出采用 MATLAB 的 datenum 串行日期格式。

    【讨论】:

    • 至少,这不适用于存储在 UNC 路径文件服务器上的文件。它返回创建日期/时间(或者可能是缓存的 mod 时间)。
    • 请忽略^^^^^^。 (无法删除。)
    猜你喜欢
    • 1970-01-01
    • 2012-05-13
    • 1970-01-01
    • 2023-03-08
    • 2010-10-05
    • 2011-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多