I'm trying to create a date columm vector that only has the month and year, since my data are average montly values over a large number of years. I can't seem to find a way to do this when days are not included. Suggestions?
13 views (last 30 days)
Show older comments
I want to create a table column that has month and year (eg. Jan-1991, Feb-1991....Dec-2015) as a date-time type. However, I can't seem to figure out how to do this without incorporating days. My data are monthly average values so I don't need days. Any suggestions on how to do this?
0 Comments
Answers (1)
Star Strider
on 10 Feb 2018
Try this:
Yr = reshape(repmat((1991:2015), 12, 1), [],1);
Mo = repmat(1:12, 1, numel(Yr)/12)';
Da = 1;
t = datetime(Yr,Mo,Da, 'Format','MMM-yyyy');
3 Comments
Star Strider
on 10 Feb 2018
My pleasure!
If my Answer helped you solve your problem, please Accept it!
Peter Perkins
on 12 Feb 2018
Thomas, SS is correct, but just to be clear what's going on: that code creates a datetime vector whose format displays only the year and month, and for most purposes (including probably yours) that's fine. But it's actually creating the sequence 1-Jan-1991, 1-Feb-1991, and only showing you what you care about.
Also, another way to construct that datetime might be to use "rollover":
d = datetime(1991,1:(25*12),1,'Format','MMM-yyyy')
See Also
Categories
Find more on Dates and Time in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!