why do I get this error? Indexing

조회 수: 2(최근 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])

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by