Selecting specific values from a structured array

21 ビュー (過去 30 日間)
HWIK
HWIK 2020 年 12 月 15 日
編集済み: Durga Yenikepalli 2020 年 12 月 17 日
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?

採用された回答

Durga Yenikepalli
Durga Yenikepalli 2020 年 12 月 17 日
編集済み: Durga Yenikepalli 2020 年 12 月 17 日
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.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by