Selecting specific values from a structured array

21 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

Durga Yenikepalli
Durga Yenikepalli il 17 Dic 2020
Modificato: Durga Yenikepalli il 17 Dic 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.

Più risposte (0)

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Prodotti


Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by