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?
15 ビュー (過去 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?
0 件のコメント
回答 (1 件)
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 件のコメント
Star Strider
2018 年 2 月 10 日
My pleasure!
If my Answer helped you solve your problem, please Accept it!
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 Center および File Exchange で Dates and Time についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!