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?

16 次查看(过去 30 天)
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?

回答(1 个)

Star Strider
Star Strider 2018-2-10
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 个评论
Peter Perkins
Peter Perkins 2018-2-12
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')

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by