Find index in struct field in which word appears

조회 수: 35 (최근 30일)
DavidL88
DavidL88 2021년 11월 9일
댓글: DavidL88 2021년 11월 9일
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'?

답변 (1개)

Stephen23
Stephen23 2021년 11월 9일
편집: Stephen23 2021년 11월 9일
"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
Stephen23
Stephen23 2021년 11월 9일
편집: Stephen23 2021년 11월 9일
"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 2021년 11월 9일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by