Find index in struct field in which word appears

33 vues (au cours des 30 derniers jours)
DavidL88
DavidL88 le 9 Nov 2021
Commenté : DavidL88 le 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'?

Réponses (1)

Stephen23
Stephen23 le 9 Nov 2021
Modifié(e) : Stephen23 le 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 = load('SampleStruct.mat').SampleStruct
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.
  3 commentaires
Stephen23
Stephen23 le 9 Nov 2021
Modifié(e) : Stephen23 le 9 Nov 2021
"Could you explain what you mean by 'storing all of the character vectors in scalar cells?'"
Every single one of your character arrays is nested inside a (superfluous?) scalar cell array, e.g.:
SubjectName = {'Subject3'};
% ^ ^ Why do you need a scalar cell array?
Ditto in your structure: every character vector is nested inside a scalar cell array: why?
"Would another way have been better?
Just using the character vectors (but this depends rather on how you process your data).
"I built that struct but I have limited experience with them/MatLab."
Your structure is fine, my comment is about the apparently superflous scalar cell arrays.
DavidL88
DavidL88 le 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
% add data to struct
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

Connectez-vous pour commenter.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by