How can I index a duration variable
7 views (last 30 days)
Show older comments
Hi, my problem is about indexing. I've got a variable of duration Time_AIS1(has all the times from messages from AIS1). Then I got Time_msg_match(it is a char that has all the line positions that I want from Time_AIS1).
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/776163/image.png)
As you can see each line gives a number from where I have to get the number in Time_AIS1. How can I use Time_msg_match to index andd get the information from Time_AIS1.
I've used str2double but I get all the values NaN and I know why I saw it in a post but how could I do it because if I want to use it as an index is has to be in numeric type
0 Comments
Accepted Answer
dpb
on 23 Oct 2021
Always much easier if you attach the actual code you've used to get to this point and a little bit, at least of the data array. Attach a .mat file is simple way to do that..
But, couple comments -- why a cell array of just one element per cell all of the same type? Why not just an array?
Probably the simplest is to use something like (I'm guessing on the datatypes here since we don't have enough to go on...)
>> tmsg=seconds(randi(30,10,1)) % dummy times duration array
tmsg =
10×1 duration array
3 sec
12 sec
18 sec
24 sec
12 sec
13 sec
15 sec
10 sec
29 sec
4 sec
>> locate=num2cell(randi(30,10,1)) % the cell array of wanted matching times
locate =
10×1 cell array
{[ 1.00]}
{[13.00]}
{[26.00]}
{[ 9.00]}
{[25.00]}
{[27.00]}
{[25.00]}
{[14.00]}
{[11.00]}
{[22.00]}
>> [lia,ib]=ismember([locate{:}],seconds(tmsg)) % find posi
lia =
1×10 logical array
0 1 0 0 0 0 0 0 0 0
ib =
0 6 0 0 0 0 0 0 0 0
>>
Salt to suit actual storage types you have or, I'd suggest to remove the cell array if isn't needed and you might be able to dispense with using a duration array here as well in lieu of just numeric times. durations have their uses, but they come with warts as well...
2 Comments
dpb
on 23 Oct 2021
ADDENDUM:
Or, of course, you could convert the lookup array into durations of the same units...
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!