フィルターのクリア

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'}
  1 件のコメント
Mehdi Jaiem
Mehdi Jaiem 2022 年 5 月 6 日
Thank you very much ! Both worked yes.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by