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 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (1)
Star Strider
am 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 Kommentare
Star Strider
am 10 Feb. 2018
My pleasure!
If my Answer helped you solve your problem, please Accept it!
Peter Perkins
am 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')
Siehe auch
Kategorien
Mehr zu Dates and Time finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!