How to convert a Year Month and Date to Day Number?

조회 수: 11 (최근 30일)
Trisha Mae Roque
Trisha Mae Roque 2022년 5월 13일
댓글: Steven Lord 2022년 5월 13일
Good Day! I'm a newbie in Matlab.
I want to ask about converting a whole column that are in date format into a day numbers. It has 2 columns and 824 lines, no headlines. I actually thought of adding another row consist of numbers 1 to 824 then removing the date column but i don't know how to do it.
Here's the sample data in csv.
2020-01-30,1.0
2020-01-31,0.0
2020-02-01,0.0
2020-02-02,1.0
2020-02-03,0.0
Here's my desired product
1,1.0
2,0.0
3,0.0
4,1.0
5,0.0
or
2020-01-30,1.0,1
2020-01-31,0.0,2
2020-02-01,0.0,3
2020-02-02,1.0,4
2020-02-03,0.0,5
Thank you in advance.
  댓글 수: 2
Rik
Rik 2022년 5월 13일
What have you tried so far?
Trisha Mae Roque
Trisha Mae Roque 2022년 5월 13일
It's just reading the file and I'm sorry for that. I don't even know how to access a column without headlines. I know how to use python but matlab is a different ground for me.

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

채택된 답변

KSSV
KSSV 2022년 5월 13일
Load the data from csv file into MATLAB using readtable
You can convert the dates into date numbers using datenum
  댓글 수: 1
Steven Lord
Steven Lord 2022년 5월 13일
Rather than using datenum to create serial date numbers I recommend using datetime to create a datetime array. Looking at some sample data:
s = ["2020-01-30"
"2020-01-31";
"2020-02-01";
"2020-02-02";
"2020-02-03"];
theFormat = 'yyyy-MM-dd'; % Use for importing and/or displaying
You can use the default display format, which in this case is different from how the data is stored in the string array s:
dt = datetime(s, 'InputFormat', theFormat)
dt = 5×1 datetime array
30-Jan-2020 31-Jan-2020 01-Feb-2020 02-Feb-2020 03-Feb-2020
Or you can specify your own display format, which displays it in the same form as the strings in s:
dt = datetime(s, 'InputFormat', theFormat, ...
'Format', theFormat)
dt = 5×1 datetime array
2020-01-30 2020-01-31 2020-02-01 2020-02-02 2020-02-03
If you're using release R2019a or later also consider using readtimetable instead of readtable.

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

추가 답변 (0개)

카테고리

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