필터 지우기
필터 지우기

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?

조회 수: 18 (최근 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
Star Strider
Star Strider 2018년 2월 10일
My pleasure!
If my Answer helped you solve your problem, please Accept it!
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