why do I get this error? Indexing

3 次查看(过去 30 天)
flashpode
flashpode 2021-10-22
评论: flashpode 2021-10-22
Hi, so I got those two variables:
Time_AIS1 is a duration variable that has many times. Times_msg_match has the lines from Time_AIS1 that I want. How can I get them without getting the problem of indexing. The message that I have is:
Unable to use a value of type cell as an index. I understand the problem but I do not get how to change to work correctly.
My code that generates Time_msg_match is:
Time_msg_match{K} = complete_match_AIS;
How could I change it to get the time directly? because I've change it the following way but I get another error:
Time_msg_match{K} = Time_AIS1(complete_match_AIS);
The error is the following: Cell contents assignment to a non-cell array.
Thank you in advance

回答(1 个)

Steven Lord
Steven Lord 2021-10-22
I'm guessing you have:
A = duration([1; 2; 3; 4], [5; 6; 7; 8], [9; 10; 11; 12])
A = 4×1 duration array
01:05:09 02:06:10 03:07:11 04:08:12
C = {'03:07:11'; '01:06:12'}
C = 2×1 cell array
{'03:07:11'} {'01:06:12'}
You can't use C as an index into A. You can't even use A as an index into A. What you can do is to use ismember after converting C into a duration array.
Cdur = duration(C)
Cdur = 2×1 duration array
03:07:11 01:06:12
[isCInA, whereIsCInA] = ismember(Cdur, A)
isCInA = 2×1 logical array
1 0
whereIsCInA = 2×1
3 0
Since the first element in whereIsCInA is 3, the first element of Cdur matches the third element of A. Since the second element of whereIsCInA is 0, the second element of Cdur is not present in A.
  1 个评论
flashpode
flashpode 2021-10-22
Hi, no my Tme_msg_match gives me the position from the time like
A = duration([1; 2; 3; 4], [5; 6; 7; 8], [9; 10; 11; 12])
C = cell{'2','3'} second and third line
and my out put I would like is tho get
D = duration([5; 6; 7; 8], [9; 10; 11; 12])

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by