我正在寻找做一些例程(更新a file.m)的MATLAB代码,如果file.csv是最近编辑的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
Run Code Online (Sandbox Code Playgroud)
时间戳提取的行似乎是不正确的MATLAB代码.其余的工作(评估外部文件字符串中的变量).
您可以使用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
Run Code Online (Sandbox Code Playgroud)
请注意,输出采用MATLAB的datenum串行日期格式.