How to preview the fields of a struct array and to select just some of them.

4 ビュー (過去 30 日間)
Manuel Arcangeletti
Manuel Arcangeletti 2018 年 4 月 10 日
回答済み: Sergey Kasyanov 2018 年 4 月 10 日
Hello, I have a struct array with a variable number of fields. In each field is stored a set of data. I'm trying to write a routine that let the user to select some of the fields in the array and then make the desired analysis on that fields.

回答 (1 件)

Sergey Kasyanov
Sergey Kasyanov 2018 年 4 月 10 日
Hi.
You can get list of fields of structure by filednames and adress to any field by that feature.
Way which you can realize interacting with a cell array with field names may vary in wide range. I suggest that you have a cell array with some strings which is your goal field names. Then:
%origin data is A and B
A={'FirstField','SecondField'};
B=struct('FirstField',Data1,...
'SecondField',Data2,...
'ThirdField',Data3);
%get field names of B
fn=fieldnames(B);
%extract data from struct
for i=1:length(A)
%check existing if A{i} field in B
if isfield(B,A{i})
%extract data from B
Data{i}=B.(A{i});
end
end

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by