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 Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by