MATLAB Answers

Adding data from table into struct

4 views (last 30 days)
DavidL88
DavidL88 on 6 Feb 2021
Edited: DavidL88 on 11 Feb 2021
Hi
I'm looking for a way to sum data in a table with data in a struct. The data in the table has to be matched with the data in the struct. I have data from a cognitive task stored in a 1x59 struct 'sFiles' (there were 59 subjects who completed the task). This task has 4 categories (original_11, original_12, original_21 & original_22), each with a timestamp of when it occurred (100 trials per category so a total of 400 trials per subject). The Subject IDs are listed under sFiles.SubjectName. In the script below, the 4 variables beginning with 'original' are each 1x100 doubles, extracted from struct sFiles, and contain the timestamps for each event for one subject. The timestamps within each one are listed sequentially in the 1x100 doubles, so first column is first trial, fifth column is fifth trial, etc.
The timestamps for these trials need to be corrected for a delay, so I need to add on a small amount of time to each one. This delay varies from trial/timestamp to trial/timestamp. I have the delay for each trial for each subject stored in a table 'T'. T.Subject has all subject IDs (400 rows for each subject), T.Conditions has the event category (11, 12, 21 & 22 (100 rows per category per subject)), T.Trial lists the number of the trial in each category (1-100) and T.Delay has all the delays that the 'original' in the struct has to corrected for (by summing them).
sFiles = bst_process('CallProcess', 'process_select_search', sFiles, [], ...
'search', '(([name CONTAINS "resample"]))');
for iFile = 1:length(sFiles)
DataMat = in_bst_data(sFiles(iFile).FileName);
original_11 = DataMat.F.events(1).times
original_12 = DataMat.F.events(2).times
original_21 = DataMat.F.events(5).times
original_22 = DataMat.F.events(6).times
end
This is an example of a few rows from the table
T =
Subject Conditions Trial Delay
"Subject008" 11 95 83
"Subject121" 12 96 84
"Subject156" 21 97 67
"Subject356" 22 98 67
Is there a way to match Subject, Conditions and Trials between the struct and Table so I can add the delays to the timestamps correctly?

Answers (1)

Image Analyst
Image Analyst on 6 Feb 2021
I didn't delve in your data (we don't even have any of it) or your code but you might want to look at grpstats() or splitapply() to do computations by groups. You might also find the functions vertcat(), struct2table(), table2struct(), or findgroups() useful.
  3 Comments
DavidL88
DavidL88 on 8 Feb 2021
I attach a sample struct (A) and table (t).
In A.events.times, the 1x100 double in rows 1, 2, 5, 6 each contain the timestamps for 100 trials across 4 conditions to be altered, corresponding with conditions 11, 12, 21, and 22 listed under 'ConditionsWhole_sample' in the table. So row 1 is 11 list in the table, row 2 is 12, row 5 is 21 and row 6 is 22. The 100 timestamps are entered sequentially in these 1x100 doubles and they correspond with the 1-100 trials which are listed sequentially under 'trial' in the table.
I need to add the values listed under 'delay' in the table to the values listed in these 1x100 double and so correct the timestamps saved here but matched for Subject (ID), Condition (11, 12, 21, 22) and trial (1-100).
Please let me know if any further information is needed.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!