hour of datenum type

4 views (last 30 days)
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 Comment
Stephen
Stephen on 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)

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 4 Mar 2021
Edited: Walter Roberson on 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 Comment
Michael Thorburn
Michael Thorburn on 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.

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 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 Comments
Walter Roberson
Walter Roberson on 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'.

Sign in to comment.

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by