hours(diff​(datetime(​[t1;t2]))) function

i have a 300 x 2 table, i want to calculate time differences and i want to count how many of them are more than 3 hours. [a, b] is the size of the table. it gives an error in the = c line & it says Unable to use a value of type duration as an index. how do i solve this?
below is the related part of the code. thank you so much
for n = 1:a
t1 = y(n,1)
t2 = y(n,2)
hours(diff(datetime(t1;t2]))) = c
if c > 3
x = x+ 1
end
end

2 件のコメント

KSSV
KSSV 2021 年 7 月 12 日
You are getting any error? What problem you are facing?
Berk Opla
Berk Opla 2021 年 7 月 12 日
yes i'm getting "Unable to use a value of type duration as an index." for the line of
hours(diff(datetime(t1;t2]))) = c

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

 採用された回答

Berk Opla
Berk Opla 2021 年 7 月 12 日
編集済み: Berk Opla 2021 年 7 月 12 日

0 投票

figured it out
hours(diff(datetime(t1;t2]))) = c is wrong
it should be c = hours(diff(datetime([t1;t2])))
for some reason

2 件のコメント

Star Strider
Star Strider 2021 年 7 月 12 日
That syntax is incorrect.
Berk Opla
Berk Opla 2021 年 7 月 12 日
yeah fixed it now. the problem was the place of the c

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

その他の回答 (2 件)

dpb
dpb 2021 年 7 月 12 日

0 投票

hours(diff(datetime(t1;t2]))) = c
You're using hours that is a builtin function as an array name on the LHS of an assignment statement and you also have an unmatched "]" bracket in the expression.
You also don't need a loop here...use MATLAB vectorized operations instead...
You don't provide what y variable contains; presuming it can be converted to a datetime, then if the answer is to determine how many of the time differences between the two columns are > 3 hours, then
nGt3=sum((datetime(y:,1)-datetime(y:,2))>3);
This presumes as your code that the first date is in the second column not the first -- one would normally think that would be the other way 'round; in which case would want to reverse the column indices in the difference calculation.
Peter Perkins
Peter Perkins 2021 年 7 月 27 日

0 投票

In addition to what others have said, you probably don't need the "hours" in
hours(diff(datetime(t1;t2])))
All that does is turn the duration that diff returns into a number. You are probably better off leaving it as duration, and replacing "3" with hours(3). More expressive.

カテゴリ

ヘルプ センター および File ExchangeDates and Time についてさらに検索

タグ

質問済み:

2021 年 7 月 12 日

回答済み:

2021 年 7 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by