char to num (of datetime)
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
i'd like to change char to a numeric value so i can add it to my matrix.
my code:
t = datetime('now','Format','ydMHHmm');
x_num = str2double(t);
result:
x_num = NaN
i'd appretiate the help
1 Commento
Davide Masiello
il 11 Ott 2022
Have you consider storing the values in a table instead? (That way, different column can be of different classes).
Risposte (1)
Dyuman Joshi
il 11 Ott 2022
datetime() does not output string, it itself is an independent data type.
t = datetime('now','Format','y/d/M HH:mm')
If you want to convert it into a numerical value, you can use datenum (though it is not recommended as per the function documentation home page)
format long
datenum(t)
1 Commento
Steven Lord
il 11 Ott 2022
Alternately, you could define how long "1" is in terms of dates and times and use datetime and duration arithmetic. For example if you want a change of 1 in the double values to which the dates and times are mapped to represent 1 day of difference in the dates and times:
t = datetime('now','Format','y/d/M HH:mm')
c = datetime('December 25, 2022','Format','y/d/M HH:mm')
delta = days(c-t);
fprintf("It is %f days until Christmas!\n", delta)
If you're counting by minutes:
delta2 = minutes(c-t);
fprintf("It is %f minutes until Christmas!\n", delta2)
Let's double check. There are this many minutes in a day:
mpd = delta2/delta
which we can check:
minutesPerDay = minutes(days(1))
I second the suggestion of @Davide Masiello to store your data in a table, though if you have time-based data a timetable array may be a better fit.
Vedere anche
Categorie
Scopri di più su Dates and Time in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!