How can I convert the year into the matlab date format if I have a big data set? I have a table:
Year Data
1999
2000
2001 ...

댓글 수: 3

Geoff Hayes
Geoff Hayes 2018년 5월 5일
Aoyi - do you want to convert these years into a serial date number (see datenum for details)? Or something else? Do you have a month and day or just the four digit year?
Aoyi
Aoyi 2018년 5월 5일
I want to convert these years into a serial date number. But is it possible to use datedum if I only have the year? As it is an error if my date is only year but it is fine if I use year and month for the date
dpb
dpb 2018년 5월 5일
편집: dpb 2018년 5월 5일
Yes, datenum(*) datevector input syntax requires the minimum 3-vector [y,m,d] so can't pass just a numeric year. As you've discovered, the easy workaround is to simply augment with the 1,1 for the month,day.
The same idea as Ameer shows below also works for datenum just for the record; I recommend using datetime for new work; it has many advantages over the venerable datenum.
>> yr=[1999:2002].';
>> yr=datenum(num2str(yr),'yyyy');
>> datestr(yr)
yr =
4×11 char array
'01-Jan-1999'
'01-Jan-2000'
'01-Jan-2001'
'01-Jan-2002'
>>
You note datenum has introduced the 1,1 mo,da value anyway so may as well use the simpler form and go on.
(*) BTW, datetime is the same; it won't supply implicit default values for missing arguments (another enhancement, maybe(???) altho how much trouble is it to add a ',1,1' to the argument list, anyway? :) )

댓글을 달려면 로그인하십시오.

 채택된 답변

Ameer Hamza
Ameer Hamza 2018년 5월 5일

0 개 추천

If you have a numeric vector containing year numbers, you can convert a vector into a datetime array object as follow
myVector = 2000:2010
datetime(string(myVector), 'Format', 'yyyy')

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Dates and Time에 대해 자세히 알아보기

태그

질문:

2018년 5월 5일

편집:

dpb
2018년 5월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by