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

1 回表示 (過去 30 日間)
JCH
JCH 2023 年 2 月 7 日
編集済み: dpb 2023 年 2 月 8 日
'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
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
end
lags=cell2mat(lags); % convert to array from cell array...
  2 件のコメント
JCH
JCH 2023 年 2 月 8 日
編集済み: JCH 2023 年 2 月 8 日
Thank you for your code. However, I got this error message:
ixStart=find(matches(trial_table.trial.message,startCode)); % the locations for the starts
Incorrect number of arguments. Each parameter name must be followed by a corresponding value.
dpb
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....
ADDENDUM:
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 =
6.00
13.00
>>
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 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by