problem with if cycle to determine the datetime

1 回表示 (過去 30 日間)
Luca Tognetti
Luca Tognetti 2023 年 2 月 3 日
コメント済み: Luca Tognetti 2023 年 2 月 8 日
hi, i'm using a function to retrieve weather data with API. I divided the function in two section, one where the outputs is the current weather and one for "past" weather. To decide which output the function has to print I made a simpole if cycle:
if fullDate == now
... %outputs current weather
else
... %outputs historic weather
end
if I try to run the single part everything works perfecty, but adding this if cylce I get this error message using fullDate = now;
Numeric input data must be a matrix with three or six columns, or else three, six, or seven separate numeric arrays. You can also create datetimes from a single numeric array using the 'ConvertFrom' parameter.
the wierd thing is that if I continue to run the code (without touching it) after a few errors sometimes it runs without any problem.
I don't get where is the problem... thanks
  2 件のコメント
Luca Tognetti
Luca Tognetti 2023 年 2 月 3 日
reading the help I changed the time declaration with:
fullDate = datetime('now');
and consequently the if cycle becomes:
if fullDate == datetime('now')
... %outputs current weather
else
... %outputs historic weather
end
now I'm not receiving that error anymore, but it seems like sometimes the if cycle macthes the time with the time input (which is "now") and most of the times it skips to the history part giving me wrong outputs. this means that the time input is not equal to "now" even if I set it...
Voss
Voss 2023 年 2 月 3 日
datetime('now') returns the current date and time, so in general it returns a different thing each time you call it, because of the passage of time.
fullDate = datetime('now')
fullDate = datetime
03-Feb-2023 17:42:50
pause(0.5)
fullDate = datetime('now')
fullDate = datetime
03-Feb-2023 17:42:51
In other words, what 'now' means changes with time, as it should.
I don't have enough information about your code or what you're trying to do to advise you about what you should do instead.

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

採用された回答

Les Beckham
Les Beckham 2023 年 2 月 3 日
編集済み: Les Beckham 2023 年 2 月 3 日
The time returned by datetime('now') will be changing as your code runs, so it seems almost impossible that your if condition will ever be met. Perhaps you should apply a tolerance or use a different condition.
Example:
t1 = datetime('now'),t2 = datetime('now')
t1 = datetime
03-Feb-2023 17:43:49
t2 = datetime
03-Feb-2023 17:43:49
isequal(t1, t2)
ans = logical
0
t1.Second
ans = 49.7308
t2.Second
ans = 49.7767
  3 件のコメント
Stephen23
Stephen23 2023 年 2 月 8 日
"idk if there's another method, but i had to convert the time in strings and compare them with 'cmpstr'."
The MATLAB approach would be to compare the difference against a tolerance, e.g.:
t1 = datetime('now');
t2 = datetime('now');
tol = minutes(1);
(t2-t1)<tol
ans = logical
1
Luca Tognetti
Luca Tognetti 2023 年 2 月 8 日
thanks! that's much clear

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by