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

10 Ansichten (letzte 30 Tage)
Trisha Mae Roque
Trisha Mae Roque am 13 Mai 2022
Kommentiert: Steven Lord am 13 Mai 2022
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 Kommentare
Rik
Rik am 13 Mai 2022
What have you tried so far?
Trisha Mae Roque
Trisha Mae Roque am 13 Mai 2022
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

KSSV
KSSV am 13 Mai 2022
Load the data from csv file into MATLAB using readtable
You can convert the dates into date numbers using datenum
  1 Kommentar
Steven Lord
Steven Lord am 13 Mai 2022
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by