hour of datenum type

5 visualizaciones (últimos 30 días)
Michael Thorburn
Michael Thorburn el 3 de Mzo. de 2021
Comentada: Michael Thorburn el 5 de Mzo. de 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 comentario
Stephen23
Stephen23 el 5 de Mzo. de 2021
Editada: Stephen23 el 5 de Mzo. de 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)

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 4 de Mzo. de 2021
Editada: Walter Roberson el 4 de Mzo. de 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.
  1 comentario
Michael Thorburn
Michael Thorburn el 5 de Mzo. de 2021
Thank you for the help. I checked "which hour" and found that, on my computer, it did use the function from the MATLAB\R2020b\toolbox\finance\calendar\hour.m
Thanks for pointing out the usage error. I wanted to understand why it worked one way on my machine and another way on my student's machines.
We can consider this closed.

Iniciar sesión para comentar.

Más respuestas (1)

Steven Lord
Steven Lord el 4 de Mzo. de 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"))
Unable to use a value of type datetime as an index.
  2 comentarios
Michael Thorburn
Michael Thorburn el 4 de Mzo. de 2021
here is a screenshot from my student's computer.
Whereas for mine I get a good answer. With this little test case, I don't think it has anything to do with defining a variable with a name that creates a conflict.
Walter Roberson
Walter Roberson el 4 de Mzo. de 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'.

Iniciar sesión para comentar.

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by