我正在尝试在循环中创建一个文件名数组,其中每个时间步都添加了5分钟:
PE.20110612.000500.tif
PE.20110612.001000.tif
PE.20110612.001500.tif
使用此代码
startYear = '2011';
startMon = '06';
startDate = '12';
startHour = '00';
startMin = '05';
endYear = '2011';
endMon = '06';
endDate = '12';
endHour = '00';
endMin = '30';
peFileNames=cell(1,20);
for k = 1:20
peFileNames{k} = strcat('PE','.',startYear,startMon,startDate,'.',startHour,startMin,'00','.tif');
startMin = startMin+5;
end
显然,这不起作用,因为我无法在每个时间步中在循环内添加5分钟。我该怎么办?
最后的想法是从StartTime转到EndTime并在数组中创建n个单元格。
答案 0 :(得分:3)
您可以使用内置的addtodate
函数(http://www.mathworks.com/help/matlab/ref/addtodate.html)代替手动实现时间添加,这将自动处理滚动到下一个字段的问题:
startDate = datenum('12-Jun-2011 00:05:00');
endDate = datenum('12-Jun-2011 00:30:00')
peFileNames=cell(1,20);
for k = 1:20
peFileNames{k} = strcat('PE.',datestr(startDate, 'yyyymmdd.HHMMSS'), '.tif');
startDate = addtodate(startDate, 5, 'minute');
end
答案 1 :(得分:2)
不使用datetime
类型(R2014b +),我创建了一个双精度矢量,然后将它们转换为字符串:
startYear = '2011' ;
startMon = '06' ;
startDate = '12' ;
%
startmin = 5 ;
freq = 5 ;
steps = 20 ;
minsraw = (startmin:freq:(freq*steps)).' ;%'
min = mod(minsraw,60) ;
hour = floor(minsraw/60) ;
%
startMin = num2str(min,'%02G') ;
startHour = num2str(hour,'%02G');
% The curly braces around .tif makes the output a cell array.
peFileNames = strcat('PE','.',startYear,startMon,startDate,'.',startHour,startMin,'00',{'.tif'});