필터 지우기
필터 지우기

Accesing a field within a row of a data structure

조회 수: 1 (최근 30일)
Mehdi Jaiem
Mehdi Jaiem 2022년 5월 6일
댓글: Mehdi Jaiem 2022년 5월 6일
I have the following code
A=[DS.Carb]>2700;
B=[DS.Vit]==1;
C=or(A,B);
Index=find(C==1);
M=[DS.User_ID(Index)];
As you can see I am trying to acces some parts of the datastructure DS and save some information deending on "Index"
for exaple if Index=[3 4 67 8] then I would like to extract the content of the User_ID corresponding to the index each time

채택된 답변

Voss
Voss 2022년 5월 6일
The correct syntax for getting the User_ID of elements at indices Index of struct array DS would be
M = [DS(Index).User_ID];
or
M = {DS(Index).User_ID};
depending on the class of the data stored in field User_ID.
For example:
DS = struct( ...
'User_ID',{1 2 3 4}, ...
'Carb',{2600 2800 2600 2800}, ...
'Vit',{0 0 1 1});
M = [DS([DS.Carb]>2700 | [DS.Vit]==1).User_ID]
M = 1×3
2 3 4
DS = struct( ...
'User_ID',{'one' 'two' 'three' 'four'}, ...
'Carb',{2600 2800 2600 2800}, ...
'Vit',{0 0 1 1});
M = {DS([DS.Carb]>2700 | [DS.Vit]==1).User_ID}
M = 1×3 cell array
{'two'} {'three'} {'four'}

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by