How to extract the time differences between two events into a table?

3 次查看(过去 30 天)
'message' contains the event name, 'time' is the timestamp for each event, and 'reltime' is the relative time of each event to the trial start.
I want to find the time lag between each ‘1002‘ and each 'ENDSACC' .
Here is an example:
For the first '1002', its time lag : 'ENDSACC'(3) (72-942= -870) 'ENDSACC'(7) (581-942= -361) 'ENDSACC'(14) (1032-942= 90).
Only the number is important so the ideal result would be like;
1002 -870 -361 90
How can I achieve such results, thank you very much.


dpb 2023-2-7
" is attached through google drive because of the file size"
It would have only taken a 100 lines or less to have sufficient to illustrate...or, actually, even the amount that you pasted an image of as text would be sufficient and somebody could do something with it...
Air code, but should be close depending upon the real data storage...I'll assume we use a table for convenience of naming variables (and, a "massage" is what you get at a day spa; a piece of data received from telemetry would be a "message")...
eventCode="1002"; % the event we're looking for; use variables don't bury magic numbers in code
startCode="ENDSACC"; % ditto start...
ixStart=find(matches(tData.message,startCode); % the locations for the starts
ixEnd=find(matches(tData.message,eventCode); % the locations for the event
nEvents=numel(ixEnd); % how many end event occurrences found
lags=cell(nEvents,1); % make a cell array of that many
tStart=tData.reltime(ixStart); % the starting times
tEnd=tData.reltime(ixEnd); % ditto end times
lags(1)={tEnd(1)-tStart(tStart<tEnd(1))}; % the first set of differences
for i=2:nEvents
lags(i)={tEnd(i)-tStart((tStart>tEnd(i-1))&(tStart<tEnd(i)))}; % and the rest
lags=cell2mat(lags); % convert to array from cell array...
  2 个评论
dpb 2023-2-8
编辑:dpb 2023-2-8
We don't have the real storage nor even the entire error message; if the data are in a table with the column/variable name of "message" as a cellstr() or string, the above should work, just fine. It appears the above was interpreted some other way -- of course you don't show how your code defines "startCode", either...
Again, attach just a sample of the data here if you want/expect somebody here to write actual code....
I made up a sample table that contained the particular string of interest as follows and works just fine...the variable msg was a machinations to get an arbitrary string of similar values; the "how" isn't important.
>> tDemo=table(msg,sort(randi(2500,size(msg))),'VariableNames',{'message','reltime'})
tDemo =
15×2 table
message reltime
___________ _______
{'1000' } 303.00
{'1001' } 419.00
{'1000' } 706.00
{'1000' } 1011.00
{'1000' } 1193.00
{'ENDSACC'} 1258.00
{'1000' } 1416.00
{'1002' } 1441.00
{'1001' } 1606.00
{'1002' } 1683.00
{'1003' } 1887.00
{'1001' } 1913.00
{'ENDSACC'} 1918.00
{'1002' } 2120.00
{'1000' } 2402.00
>> startCode='ENDSACC';
>> ixStart=find(matches(tDemo.message,startCode))
ixStart =
So, there's no issue regarding using a search on those data that would lead to such a message with such a data structure -- you've got something else going on but we can't really see what without all the code in context to see what you actually did; there's not sufficient code inline with the full error message text to be able to debug what may have actually caused the error (since we really don't even know what it was that threw that particular error--it's not one I recognize as being a common one that can be diagnosed from just the text string itself).


更多回答(0 个)





Community Treasure Hunt

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

Start Hunting!

Translated by