Selecting specific values from a structured array

17 Ansichten (letzte 30 Tage)
HWIK
HWIK am 15 Dez. 2020
Bearbeitet: Durga Yenikepalli am 17 Dez. 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?

Akzeptierte Antwort

Durga Yenikepalli
Durga Yenikepalli am 17 Dez. 2020
Bearbeitet: Durga Yenikepalli am 17 Dez. 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.

Weitere Antworten (0)

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by