www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/CreateEquallySpacedSequenceOfDatetimeValuesExample.m

    %% Sequence of Datetime or Duration Values Between Endpoints with Step Size
% This example shows how to use the colon (:) operator to generate
% sequences of |datetime| or |duration|
% values in the same way that you create regularly spaced numeric vectors.
%% Use Default Step Size
% Create a sequence of datetime values starting from November 1, 2013 and
% ending on November 5, 2013. The default step size is one calendar day.

% Copyright 2015 The MathWorks, Inc.

t1 = datetime(2013,11,1,8,0,0);
t2 = datetime(2013,11,5,8,0,0);
t = t1:t2
%% Specify Step Size
% Specify a step size of 2 calendar days using the |caldays| function.
t = t1:caldays(2):t2
%%
% Specify a step size in units other than days. Create a sequence of
% datetime values spaced 18 hours apart.
t = t1:hours(18):t2

%%
% Use the |years|, |days|, |minutes|, and |seconds| functions to create
% datetime and duration sequences using other fixed-length date and time units. Create a sequence
% of duration values between 0 and 3 minutes, incremented by 30 seconds.
d = 0:seconds(30):minutes(3)

%% Compare Fixed-Length Duration and Calendar Duration Step Sizes
% Assign a time zone to |t1| and |t2|. In the |America/New_York| time zone,
% |t1| now occurs just before a daylight saving time change.
t1.TimeZone = 'America/New_York';
t2.TimeZone = 'America/New_York';
%%
% If you create the sequence using a step size of one calendar day,
% then the difference between successive |datetime| values is not always 24
% hours.
t = t1:t2;
dt = diff(t)
%%
% Create a sequence of datetime values spaced one fixed-length day apart,
t = t1:days(1):t2
%%
% Verify that the difference between successive |datetime| values is 24
% hours.
dt = diff(t)

%% Integer Step Size
% If you specify a step size in terms of an integer, it is interpreted as a number of 24-hour days.
t = t1:1:t2