hour of datenum type
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Michael Thorburn
le 3 Mar 2021
Commenté : Michael Thorburn
le 5 Mar 2021
The following set of commands works fine on my computer (PC, Windows 10, MATLAB R2020b)
datetime("now")
month(datetime("now"))
month(datenum(datetime("now")))
day(datetime("now"))
day(datenum(datetime("now")))
hour(datetime("now"))
hour(datenum(datetime("now")))
But in every case where you try to compute the month, day, or hour of a datenum type, my students get a MATLAB error. What must be different between their computer/MATLAB configuration and mine. (They appear also to use Windows 10 and MATLAB R2020b).
1 commentaire
Stephen23
le 5 Mar 2021
Modifié(e) : Stephen23
le 5 Mar 2021
Note that MATLAB's month, day, and hour documentation all state that their first input must be a datetime array.
Calling those functions with any other data type (e.g. double) is unlikely to be useful, and is certainly not documented.
The finance toolbox unfortunately includes functions named month and day: do you have the finance toolbox installed?
Is there a particular reason why your students cannot use the standard, reliable, recommended method to get date/time units from a serial date number? (i.e. convert to a date vector using datevec)
Réponse acceptée
Walter Roberson
le 4 Mar 2021
Modifié(e) : Walter Roberson
le 4 Mar 2021
datetime("now")
Okay, creates a datetime object and lets it be displayed
month(datetime("now"))
Okay, creates a datetime object and operates on it with the method named month that is defined for datetime objects.
month(datenum(datetime("now")))
Creates a datetime object, and converts it to a serial date number, which is a double. Then it tries to find a method named month for doubles, or a function named month somewhere on the MATLAB path. On the student's computers, neither of those exist because Mathworks does not provide any month function in general, or any method named month that is applicable to datatype double. This leads to the error message displayed. The error message is the expected outcome under R2020b or earlier unless the user has a third-party function named month on the path, or unless the user has a third-party function named datenum on their path that is supplying a datetime object instead of a double.
day(datetime("now"))
Okay, constructs a datetime object, and invokes the method day that applies to datetime objects.
day(datenum(datetime("now")))
Creates a datetime object, and converts it to a serial date number, which is a double. Then it tries to find a method named month for doubles, or a function named day somewhere on the MATLAB path. This is the same situation as described above: the Mathworks-supplied datenum returns double and there is no method named day for datatype double.
hour(datetime("now"))
hour(datenum(datetime("now")))
Same, same.
datenum as supplied by Mathworks does not create objects that have day or hour or month methods defined for them.
I recommend checking
which datenum
on the computer that is being executed on. The expected results should look like
/Applications/MATLAB_R2020b.app/toolbox/matlab/timefun/datenum.m
/Applications/MATLAB_R2020b.app/toolbox/matlab/datatypes/duration/@duration/duration.m % duration method
/Applications/MATLAB_R2020b.app/toolbox/matlab/datatypes/datetime/@datetime/datetime.m % datetime method
/Applications/MATLAB_R2020b.app/toolbox/matlab/bigdata/@tall/datenum.m % tall method
except with a root directory appropriate for the installation.
By the way, the way to get month / day / hour information from a serial date number is to call datevec() and extract the relevant column from the output.
Plus de réponses (1)
Steven Lord
le 4 Mar 2021
What is the full and exact text of the error messages they receive? Show all the text displayed in red (and if there is any text displayed in orange, show that too.)
My guess is that they have created variables named hour, day, and/or month that are taking precedence over the hour, day, and/or month functions. In that case attempting to call the function with a datetime as input would be interpreted as an attempt to index into the variable, and that won't work.
month = 1:10;
month(datetime("today"))
2 commentaires
Walter Roberson
le 4 Mar 2021
Well, that was unnecessarily hard to read :(
The read text says
Check for missing argument or incorrect argument data type in call to function 'month'.
Voir également
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!