Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 如何正确生成带有5分钟时间戳的文件名?_Matlab_Filenames - Fatal编程技术网

Matlab 如何正确生成带有5分钟时间戳的文件名?

Matlab 如何正确生成带有5分钟时间戳的文件名?,matlab,filenames,Matlab,Filenames,我使用下面的代码根据时间步数每五分钟生成一次文件名。但它不能正常工作。如果您打开precipFileNames,您将看到中间,代码每5分钟停止一次,而是每5分钟1秒执行一次,生成如下文件名: E:\MRMS\2004\PRECIPRATE.20040402.051959.tif 如何才能正确地执行此操作 timeSteps = 417; pathstr = 'E:\MRMS\2004'; startTime = 7.320395312500000e+05; peakTime2 = 7.3204

我使用下面的代码根据时间步数每五分钟生成一次文件名。但它不能正常工作。如果您打开precipFileNames,您将看到中间,代码每5分钟停止一次,而是每5分钟1秒执行一次,生成如下文件名:

E:\MRMS\2004\PRECIPRATE.20040402.051959.tif
如何才能正确地执行此操作

timeSteps = 417;
pathstr = 'E:\MRMS\2004';
startTime = 7.320395312500000e+05;
peakTime2 = 7.320400104166666e+05;
precipFileNames=cell(timeSteps,1);

for l = 1:timeSteps
       %precipFileNames{m} = strcat(fileparts(refFile), filesep, datestr(startTime, 'yyyy'), filesep,'PRECIPRATE.',datestr(startTime, 'yyyymmdd.hhMMss'), '.tif');
       precipFileNames{l} = strcat(pathstr(1:end-4), datestr(startTime, 'yyyy'), filesep, 'PRECIPRATE.',datestr(peakTime2, 'yyyymmdd.hhMMss'), '.tif');
       peakTime2 = addtodate(peakTime2, -5, 'minute');  %No. of times we go back in time from peak time
 end

日期/时间使用浮点数在内部存储。每次通过循环,您都会将一个非常小的值(5分钟,
0.0035
)添加到一个相对较大的值(
7e05
-ish),这会导致累积。这些错误表现为与预期值略有差异

由于在循环中反复执行此加法(到
peakTime2
),因此一次迭代的浮点错误会放大,因为下一次迭代取决于上一次迭代的结果

我不会持续更新
peakTime2
而是每次通过循环改变增量值并将其应用于原始的datetime对象。这样就不会累积误差,只需执行一次减法即可获得特定值

for k = 1:timeSteps
    % Compute how many minutes to shift this iteration
    shift = -5 * (k - 1);

    % Apply the shift to the reference time
    thistime = addtodate(peakTime2, shift, 'minute');

    % Create the filename
    precipFileNames{k} = strcat(pathstr(1:end-4), ...
                                datestr(startTime, 'yyyy'), ...
                                filesep, ...
                                'PRECIPRATE.', ...
                                datestr(thistime, 'yyyymmdd.hhMMss'), ...
                                '.tif');
end

作为旁注,为了便于人们阅读您的代码,我强烈建议不要使用
l
作为变量,因为它看起来很像
1

日期/时间是使用浮点数在内部存储的。每次通过循环,您都会将一个非常小的值(5分钟,
0.0035
)添加到一个相对较大的值(
7e05
-ish),这会导致累积。这些错误表现为与预期值略有差异

由于在循环中反复执行此加法(到
peakTime2
),因此一次迭代的浮点错误会放大,因为下一次迭代取决于上一次迭代的结果

我不会持续更新
peakTime2
而是每次通过循环改变增量值并将其应用于原始的datetime对象。这样就不会累积误差,只需执行一次减法即可获得特定值

for k = 1:timeSteps
    % Compute how many minutes to shift this iteration
    shift = -5 * (k - 1);

    % Apply the shift to the reference time
    thistime = addtodate(peakTime2, shift, 'minute');

    % Create the filename
    precipFileNames{k} = strcat(pathstr(1:end-4), ...
                                datestr(startTime, 'yyyy'), ...
                                filesep, ...
                                'PRECIPRATE.', ...
                                datestr(thistime, 'yyyymmdd.hhMMss'), ...
                                '.tif');
end

作为旁注,为了便于人们阅读您的代码,我强烈建议不要使用
l
作为变量,因为它看起来很像
1

@maximusdooku我已经用执行的代码更新了答案this@maximusdooku我已经用执行此操作的代码更新了答案