Selecting specific values from a structured array

21 visualizaciones (últimos 30 días)
HWIK
HWIK el 15 de Dic. de 2020
Editada: Durga Yenikepalli el 17 de Dic. de 2020
Hi, I have a structured array with 10 fields, each one of which is a 8x1 array of symbolic values. Is there any way I can save to another array the 3rd value only of each one of these fields?

Respuesta aceptada

Durga Yenikepalli
Durga Yenikepalli el 17 de Dic. de 2020
Editada: Durga Yenikepalli el 17 de Dic. de 2020
Hi Oliver,
I understand that you want to select specific element from a structured array and save it to another array.
We can use ‘a = extractfield(S,name)’ function which returns the field values specified by the field name of structure s, and then try to extract 3rd element from the extracted field values and save to another array. Refer below code.
% Example code
% structured array with 10 fields
s = struct('f1', a1,'f2', a2, 'f3', a3, 'f4', a4, 'f5', a5,'f6', a6, 'f7', a7, 'f8', a8, 'f9', a9, 'f10', a10);
% extracting field names
fieldNames = fieldnames(s);
% preallocating array for better performance
finalArray = zeros(1,10);
% iterate and extract 3rd value from each field
for n =1:numel(fieldNames)
f = extractfield(s, fieldNames{n});
finalArray(n) = f(3);
end
Thanks.

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by