How to match trial number to time stamps given two sets of data?

3 Ansichten (letzte 30 Tage)
Jacob
Jacob am 18 Aug. 2021
Kommentiert: Jacob am 22 Aug. 2021
I have a set of data with time stamps every couple of milliseconds that I need to add a trial number to using another set of data that gives me the onset of the trial. What I would like to do is add a column to the time stamped data that gives the trial number for each sample taken. The data that gives the trial onset has columns for the cue onset, stimulus onset, and response time. I would like to measure the trial from the cue onset to the next cue, and then add another column that indicates where in the trial this occurs.
For example, say A is my timestamped data (A in practice would have other columns with unrelated variables) and B is the table that indicates when these occur. The column in A is just the time stamp. The columns for B are- B(:,1)=cue (when I want the trial to start), B(:,2)=stimulus onset, B(:,3)=when the response occured
I would like to get a table (call it C) where the columns would be C(:,1)=timestamp from A, C(:,2)= trial number, C(:,3)=where in the trial the stamp is. C for this would look like:
I want the trial to start at cue onset and then end at the next trials cue onset. C(:,3) would indicate where in the trial the stamp is so that 0 could be from cue and stimulus onset, 1 could be from stimulus onset to response, and 2 could be from response time to the next cue.
My table also has some time stamps from before the first trial cue, so I was also wondering if there was an easy way to get rid of the timestamped data before this first cue?
Thank you for any help!!

Akzeptierte Antwort

Eric Sofen
Eric Sofen am 19 Aug. 2021
Assuming your timestamps are durations (you probably want A to be a timetable with duration row times), you can use the isbetween function.
whichTrial = isbetween(A,B(:,1)',[B(2:end,1)', inf]);
whichTrial is now a logical array, with each column representing the trial and each row corresponding to the timestamps in A. Then use find to
[~,trial] = find(whichTrial);
A.trial = trial;
You could then do something similar for the information that you illustrated in C(:,3).
  2 Kommentare
Eric Sofen
Eric Sofen am 19 Aug. 2021
Bearbeitet: Eric Sofen am 19 Aug. 2021
Hold on! I just realized there are better ways to do this!
You can use the times in B as the bin edges in discretize.
But even better, if you make A and B both timetables, you could also do it in a couple steps using synchronize.
A = timetable(rand(100,1),'SampleRate',1);
B = table(seconds([0;30;80]), seconds([10;45;88]), seconds([20;70;95]),'VariableNames',{'cue','stimulus','response'});
B.Trial = (1:height(B))';
B1 = stack(B,1:3,"NewDataVariableName","Time","IndexVariableName","State"));
B1 = table2timetable(B1)
>> B1
B1 =
9×2 timetable
Time Trial State
______ _____ ________
0 sec 1 cue
10 sec 1 stimulus
20 sec 1 response
30 sec 2 cue
...
>> synchronize(A,B1,"first","previous")
ans =
100×3 timetable
Time Var1 Trial State
______ ________ _____ ________
0 sec 0.58225 1 cue
1 sec 0.54074 1 cue
2 sec 0.86994 1 cue
3 sec 0.26478 1 cue
4 sec 0.31807 1 cue
5 sec 0.11921 1 cue
6 sec 0.93983 1 cue
7 sec 0.64555 1 cue
8 sec 0.47946 1 cue
9 sec 0.63932 1 cue
10 sec 0.54472 1 stimulus
11 sec 0.64731 1 stimulus
12 sec 0.54389 1 stimulus
13 sec 0.72105 1 stimulus
14 sec 0.5225 1 stimulus
...
Jacob
Jacob am 22 Aug. 2021
Thank you!! That was exactly what I needed.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by