Borrar filtros
Borrar filtros

Accesing a field within a row of a data structure

1 visualización (últimos 30 días)
Mehdi Jaiem
Mehdi Jaiem el 6 de Mayo de 2022
Comentada: Mehdi Jaiem el 6 de Mayo de 2022
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

Respuesta aceptada

Voss
Voss el 6 de Mayo de 2022
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'}

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by