Did I do anything wrong when I used the "second" function?

1 回表示 (過去 30 日間)
원석 서
원석 서 2022 年 2 月 4 日
編集済み: Stephen23 2022 年 2 月 4 日
I have the date data.
I'm using a program created by a previous person.
However, the following problems arise.
CurrentTime = datenum("2022-02-04 15:23:26");
INPUTDATA.SS = second(CurrentTime);
Check for incorrect argument data type or missing argument in call to function 'second'.
I have to keep using this, but I want to know what's wrong.
  2 件のコメント
KSSV
KSSV 2022 年 2 月 4 日
What does
which second
/MATLAB/toolbox/finance/calendar/second.m
show?
Stephen23
Stephen23 2022 年 2 月 4 日
編集済み: Stephen23 2022 年 2 月 4 日
@KSSV: simply showing the first result is not sufficient to determine which overloaded function will be called for a specific input class, for that you need to use the -all flag:
which second -all
/MATLAB/toolbox/finance/calendar/second.m /MATLAB/toolbox/matlab/datatypes/datetime/@datetime/datetime.m % Shadowed datetime method /MATLAB/toolbox/matlab/bigdata/@tall/second.m % Shadowed tall method /MATLAB/toolbox/parallel/parallel/@codistributed/second.m % Shadowed codistributed method
Then we can clearly see that the remaining functions are really methods of specific classes (not double). If the user does not have the financial toolbox then none of the remaining class methods accept a DOUBLE input.

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

採用された回答

Stephen23
Stephen23 2022 年 2 月 4 日
編集済み: Stephen23 2022 年 2 月 4 日
"Did I do anything wrong when I used the "second" function?"
Yes, you supplied the wrong input class: as its documentation clearly states the function SECOND is only defined for DATETIME objects. It will not work with serial date numbers (which are double class), which is what you tried.
You should use a DATETIME object and avoid deprecated DATENUM:
T = datetime("2022-02-04 15:23:26")
T = datetime
04-Feb-2022 15:23:26
S = second(T)
S = 26
Do not mix up the functions:
  • SECOND (which only works with DATETIME objects, but is part of MATLAB)
  • SECOND (which is part of the the Financial Toolbox, and you probably do not have)
  • SECONDS (which will accept a DOUBLE input and return a DURATION object, but give a meaningless output if you try it with a serial date number)
  • any other function you might have on your path named SECOND...
  3 件のコメント
Stephen23
Stephen23 2022 年 2 月 4 日
編集済み: Stephen23 2022 年 2 月 4 日
@KSSV: you showed in your earlier comment that you are using the Financial Toolbox function SECOND:
which does accept a serial date number input argument. But clearly the OP does not have this, although the person who originally wrote the code may have had. I would not presume, as you do, that a user has all toolboxes installed.
That confusion is one of the disadvantages of MATLAB's lack of proper packages.
원석 서
원석 서 2022 年 2 月 4 日
Thank you!!!!!
I solved it with your advice!!!!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTime Series Objects についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by