How to convert to datenumber from two cell values

1 vue (au cours des 30 derniers jours)
Duncan Wright
Duncan Wright le 15 Fév 2018
Commenté : Duncan Wright le 15 Fév 2018
date = cell2mat(tmp{1}(1))
time = cell2mat(tmp{1}(2))
dt = datenum([date time], 'dd:mm:yyyy HH:MM:SS')
Error using datenum (line 181)
DATENUM failed.
Caused by:
Error using dtstr2dtnummx
Failed to convert from text to date number.
What exactly am i doing wrong here?
Thanks in advance
  5 commentaires
Jos (10584)
Jos (10584) le 15 Fév 2018
As Jan already hinted at, add a space between date and time
str = [date ' ' time]
datum(str, 'dd:mm:yyyy HH:MM:SS')
Jan
Jan le 15 Fév 2018
Modifié(e) : Jan le 15 Fév 2018
What exactly is a "cell structure"? The explanation is not clear:
tmp{1}(1) = 26:05:2017
This is no valid Matlab syntax, therefore the readers have to guess, what it exactly means. Maybe it is a string object:
tmp{1}(1) = "26:05:2017"
Or a cell string:
tmp{1}(1) = {'26:05:2017'}
Please do not let us guess the details. I could post some code and test it, if you show us some valid Matlab code to create the inputs.
Are you sure that the date is written with colons? This is rather unusual.

Connectez-vous pour commenter.

Réponse acceptée

Peter Perkins
Peter Perkins le 15 Fév 2018
In any case, if you are using a recent Version of MATLAB (R2014b or later), consider moving to datetimes, not datenums:
>> date = '26:05:2017';
>> time = '16:05:30';
>> dt = datetime([date ' ' time],'Format','dd:MM:yyyy HH:mm:ss')
dt =
datetime
26:05:2017 16:05:30
>> dt = datetime([date ' ' time],'InputFormat','dd:MM:yyyy HH:mm:ss')
dt =
datetime
26-May-2017 16:05:30

Plus de réponses (0)

Catégories

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

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by