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

11 vues (au cours des 30 derniers jours)
Trisha Mae Roque
Trisha Mae Roque le 13 Mai 2022
Commenté : Steven Lord le 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 commentaires
Rik
Rik le 13 Mai 2022
What have you tried so far?
Trisha Mae Roque
Trisha Mae Roque le 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.

Connectez-vous pour commenter.

Réponse acceptée

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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Dates and Time dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by