1

How do I generate this 480x1 array of date-times in Matlab?

1982-01-01
1982-02-01
1982-03-01
1982-03-01 
1982-04-01
.
.
.
2015-12-01

1 Answer 1

3

This is made easy with the datetime function (introduced in R2014b) and following the documentation to Generate Sequence of Dates and Time.

% MATLAB 2019a
t1 = datetime(1982,1,1);
t2 = datetime(2015,12,1);
t = t1:t2;
t = t(:);      % Force column

Alternatively, you can specify the number of linearly-spaced points between two dates using the linspace command.

t_alt = linspace(t1,t2,480).';

You can convert to the y-M-d format you specified with datetime as well.

t_format = datetime(t,'Format','y-M-d')

References:
1. Dates and Time (in MATLAB)
2. Represent Dates and Times in MATLAB
3. Set Date and Time Display Format
4. Generate Sequence of Dates and Time

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.