hour of datenum type

5 ビュー (過去 30 日間)
Michael Thorburn
Michael Thorburn 2021 年 3 月 3 日
コメント済み: Michael Thorburn 2021 年 3 月 5 日
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 件のコメント
Stephen23
Stephen23 2021 年 3 月 5 日
編集済み: Stephen23 2021 年 3 月 5 日
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)

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2021 年 3 月 4 日
編集済み: Walter Roberson 2021 年 3 月 4 日
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 件のコメント
Michael Thorburn
Michael Thorburn 2021 年 3 月 5 日
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.

サインインしてコメントする。

その他の回答 (1 件)

Steven Lord
Steven Lord 2021 年 3 月 4 日
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 件のコメント
Michael Thorburn
Michael Thorburn 2021 年 3 月 4 日
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 2021 年 3 月 4 日
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'.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by