year function does not read properly
5 views (last 30 days)
Show older comments
dear friends,
I need to use year function. in excel file I prepared the data in date format. Then I changed it to number and save. Then the data is imported to Matlab. The true value of year(A(1,1)) is 2005 but matlab gives me 105.
Can anyone help?
Best Regards
1 Comment
Guillaume
on 24 Nov 2019
The year function works fine so there must be a problem with what you're doing. Unfortunately, it's not very clear, so please attach a demo excel file and show us the code you've been using.
Answers (1)
Stijn Haenen
on 24 Nov 2019
Edited: Guillaume
on 24 Nov 2019
year(input) tells you in which year the day of the input is, where the input is the day number, starting from first of january 1BC. So the first 366 days are in year 0. Day number 728647 for example is 19-Dec-1994, so year(728647)=1994.
Stijn's other answer added to the first answer (guillaume):
In excel, the first day is the first of january 1900, i think.
1 Comment
Guillaume
on 24 Nov 2019
Please stick to one answer. You can edit your answer or comment on it.
"so year(728647)=1994." Hum,
>> year(728647)
Undefined function 'year' for input arguments of type
'double'.
year requires an array of class datetime. It doesn't work on numeric arrays.
"In excel, the first day is the first of january 1900"
It's a bit more complicated. An excel date is the number of days since 0-Jan-1900, but excel incorrectly assumes that 1900 is a leap year, so you've got an extra day for all dates after 28-Feb-1900.
In any case, it's a bit irrelevant, matlab is perfectly capable of importing excel dates directly as datetime so you don't have to worry about any of this.
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!