Index a row based on value

Tags: arrays matlab
Question!

I have a cell array called BodyData in MATLAB that has around 139 columns and 3500 odd rows of skeletal tracking data. I need to extract all rows between two string values (these are timestamps when an event happened) that I have e.g.

BodyData{}= 
Column 1             2       3   
'10:15:15.332'   'BASE05'   ... 
...
'10:17:33:230'   'BASE05'   ...

The two timestamps should match a value in the array but might also be within a few ms of those in the array e.g.

TimeStamp1 = '10:15:15.560'
TimeStamp2 = '10:17:33.233'

I have several questions!

How can I return an array for all the data between the two string values plus or minus a small threshold of say .100ms?

Also can I also add another condition to say that all str values in column2 must also be the same, otherwise ignore? For example, only return the timestamps between A and B only if 'BASE02'

Many thanks,



Answers

The best approach to the first part of your problem is probably to change from strings to numeric date values. In Matlab this can be done quite painlessly with datenum.

For the second part you can just use logical indexing... this is were you put a condition (i.e. that second columns is BASE02) within the indexing expression.

A self-contained example:

% some example data:
BodyData = {'10:15:15.332', 'BASE05', 'foo';... 
            '10:15:16.332', 'BASE02', 'bar';... 
            '10:15:17.332', 'BASE05', 'foo';... 
            '10:15:18.332', 'BASE02', 'foo';... 
            '10:15:19.332', 'BASE05', 'bar'};

% create column vector of numeric times, and define start/end times
dateValues = datenum(BodyData(:, 1), 'HH:MM:SS.FFF');
startTime = datenum('10:15:16.100', 'HH:MM:SS.FFF');
endTime = datenum('10:15:18.500', 'HH:MM:SS.FFF');

% select data in range, and where second column is 'BASE02'
BodyData(dateValues > startTime & dateValues < endTime & strcmp(BodyData(:, 2), 'BASE02'), :)

Returns:

ans =
'10:15:16.332' 'BASE02' 'bar'
'10:15:18.332' 'BASE02' 'foo'

References: datenum manual page, matlab help page on logical indexing.



Your question starts unusually, because you show an array containing a single string that is comma-separated, rather than an array of individual strings.

You could try something like the following:

$arr = [];
$string = ['miniTrack, boxtrack'];
//split the one string into an array, trimming spaces
$vars= array_map('trim', explode(',', $string[0]));
foreach($vars as $var) {
    $arr[] = $var; //add the var to the array 
}

print_r($arr);

Array
(
    [0] => miniTrack
    [1] => boxtrack
)

And if you need to create a variable for each item, you can create "variable variables":

foreach($vars as $var) {
    $my_var = $$var; //variable variable
}


You can convert your strings to array like this. It may be not ideal but you can use try this.

$string = '[miniTrack, boxTrack]';
$string = str_replace(array('[',']'),array('', '' ), $string);
$array = explode(',',$string);

Here you can iterate your array whatever you want.



This video can help you solving your question :)
By: admin