Find index in struct field in which word appears
38 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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'?
0 Kommentare
Antworten (1)
Stephen23
am 9 Nov. 2021
Bearbeitet: Stephen23
am 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
SubjectName = {'Subject3'};
idx = strcmp([S.Subject],SubjectName) % logical index
ndx = find(idx) % linear index
Why are you storing all of the character vectors in scalar cells? There does not seem to be any point in that.
3 Kommentare
Stephen23
am 9 Nov. 2021
Bearbeitet: Stephen23
am 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.
Siehe auch
Kategorien
Mehr zu Spreadsheets finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!