Find index in struct field in which word appears

4 views (last 30 days)
DavidL88 on 9 Nov 2021
Commented: DavidL88 on 9 Nov 2021
I have a 1×1 cell array - SubjectName = {'Subject3'}. How do I find the row in which this appears in the attached struct in which the subjects are listed in the field 'Subject'?

Stephen on 9 Nov 2021
Edited: Stephen on 9 Nov 2021
"How do I find the row in which this appears in the attached struct in which the subjects are listed in the field 'Subject'?"
Your structure has size 1x3, so it actually has one row and three columns.
S = 1×3 struct array with fields:
Subject Events
SubjectName = {'Subject3'};
idx = strcmp([S.Subject],SubjectName) % logical index
idx = 1×3 logical array
0 0 1
ndx = find(idx) % linear index
ndx = 3
Why are you storing all of the character vectors in scalar cells? There does not seem to be any point in that.
DavidL88 on 9 Nov 2021
I had an Excel table which I converted to a struct for my analysis. I used this script. Example of Excel table below. This table had multiple ROI with associated latencies for 4 events (11_right, etc) for each Subject. I wanted a struct with this contained so I could run a loop to import this data into another software that runs on MatLab. Is there an easier or more straightforward way to organise this?
[testID, SubjectNames] = findgroups(MyData.ID);
[testID, Events] = findgroups(MyData.Outcome);
[testID, ERPs] = findgroups(MyData.ROI);
Events = num2cell(Events)
% create struct
for i = 1:length(SubjectNames)
for i2 = 1:length(Events)
B(i).Subject = SubjectNames(i)
B(i).Events = Events
B(i).Events(i2,2) = {zeros(1,100)}
end
end
for i = 1:length(SubjectNames)
for i2 = 1:length(Events)
b = (ismember(MyData.ID, B(i).Subject)) & (ismember(MyData.Outcome, B(i).Events{i2}))
idx = find(b)
B(i).Events(i2,2) = {MyData.ROI(idx)}
B(i).Events{i2,2}(:,2) = num2cell(MyData.Latency(idx))
end
end
%This is an example of the Excel table
ID Outcome ROI Latency
Subject1 11_right P1 132.8125
Subject1 11_right P2 226.5625
Subject1 11_right N1 242.1875
Subject1 11_right P3 324.21875
Subject1 12_right P1 117.1875
Subject1 12_right P2 199.21875
Subject1 12_right N1 203.125