hour of datenum type

5 visualizzazioni (ultimi 30 giorni)
Michael Thorburn
Michael Thorburn il 3 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 Commento
Stephen23
Stephen23 il 5 Mar 2021
Modificato: Stephen23 il 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)

Accedi per commentare.

Risposta accettata

Walter Roberson
Walter Roberson il 4 Mar 2021
Modificato: Walter Roberson il 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.
  1 Commento
Michael Thorburn
Michael Thorburn il 5 Mar 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.

Accedi per commentare.

Più risposte (1)

Steven Lord
Steven Lord il 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"))
Unable to use a value of type datetime as an index.
  2 Commenti
Michael Thorburn
Michael Thorburn il 4 Mar 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 il 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'.

Accedi per commentare.

Categorie

Scopri di più su Dates and Time in Help Center e File Exchange

Prodotti


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by