logical indexing with a structure

조회 수: 22 (최근 30일)
MathWorks Support Team
MathWorks Support Team 2018년 8월 15일
답변: MathWorks Support Team 2018년 8월 15일
I have a structure, "s", and I am trying to use logical indexing to create a new structure, "s1", for when the field, "field1", is equivalent to 1 and a new structure, "sc" for when the field, "field2", is equivalent to 'c'. How can I do this?
>> s = struct('field1', {1,2,3,4}, 'field2', {'ab', 'b', 'c', 'c'});

채택된 답변

MathWorks Support Team
MathWorks Support Team 2018년 8월 15일
Logical indexing with structures is similar to other logical indexing in MATLAB. The one difference is when you are trying to use fields in the equivalence statement. This is because s.field1 has multiple outputs. To get around this, simply put brackets [] or {} around s.field1 in order to get one output that is an array or cell.
>> s1 = s([s.field1]==1);
>> sc = s(strcmp({s.field2}, 'c'))

추가 답변 (0개)

카테고리

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

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by