Using loop to extract fields from a struct

115 ビュー (過去 30 日間)
MiauMiau
MiauMiau 2017 年 2 月 28 日
Hello
I am using the following code:
names = fieldnames(data); % extract names of features
for i = 1:5
class(i) = data.names{i};
end
where data is a struct, and names obviously contains the fieldnames. What I basically want to do is to extract certain field from data, and put them into an array. How can this be done (my attribution in the second last line does not work). Many thanks
  1 件のコメント
Jan
Jan 2017 年 2 月 28 日
Avoid to shadow the Matlab command "class" by a varaible.

サインインしてコメントする。

回答 (3 件)

Guillaume
Guillaume 2017 年 2 月 28 日
See this page from the doc. Basically,
class(i) = data.(names{i});
  2 件のコメント
MiauMiau
MiauMiau 2017 年 2 月 28 日
That doesn't do what I want at all. For instance if i = 1, it does not take the first string from the names cell, but it takes the first field of the data struct...
Guillaume
Guillaume 2017 年 2 月 28 日
編集済み: Guillaume 2017 年 2 月 28 日
I do not understand what you want then. Can you give a small example of an input and desired output.
The first string from the names cell array is simply:
names{1}

サインインしてコメントする。


KSSV
KSSV 2017 年 2 月 28 日
N = 10 ;
data = struct ;
for i = 1:N
data(i).name = randseq(5) ;
data(i).Age = randi(100,1) ;
data(i).city = randseq(10) ;
end
% pick the Ages
fields = fieldnames(data); % extract names of features
Age_array = zeros(N,1) ;
for i = 1:N
Age_array(i) = getfield(data(i), fields{2});
end
  1 件のコメント
APARNA HARICHANDRAN
APARNA HARICHANDRAN 2020 年 8 月 16 日
Thanks a lot! This works

サインインしてコメントする。


maxroucool
maxroucool 2017 年 10 月 19 日
Hi MiauMiau,
I am little late, but i guess I have the same problem. If names were numbers, the answer would be :
class = [data.names];
But in our case Names are letters and it generates a single var concatenating all the names together... Does anyone has the solution?
Thanks,
Max
  3 件のコメント
maxroucool
maxroucool 2017 年 10 月 19 日
Hi Jan,
I did not hijack, since I have the same problem and it was not answered... Anyway, thanks for your answer it's perfect!
Max
Guillaume
Guillaume 2017 年 10 月 19 日
@maxroucoul,
The box where you wrote your question is clearly marked "Answer this question". You are not answering the question, therefore you shouldn't have used that box.
The only way people get rewarded (increase in reputation) for answering other people questions is when people mark their answer as accepted or vote for their answer. Because you hijacked the answer box, we're forced to answer you in a comment which cannot be voted on or accepted. Hence we don't get any reward.
So please, in the future ask your question as a new question. If it relates to another question you can link to it in your own question and leave a comment in the other question linking to yours.

サインインしてコメントする。

カテゴリ

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