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

11 次查看(过去 30 天)
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 个评论
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