Selecting specific values from a structured array

21 vues (au cours des 30 derniers jours)
HWIK
HWIK le 15 Déc 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?

Réponse acceptée

Durga Yenikepalli
Durga Yenikepalli le 17 Déc 2020
Modifié(e) : Durga Yenikepalli le 17 Déc 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.

Plus de réponses (0)

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by