how to set multiple fields in a struct without using a loop

3 ビュー (過去 30 日間)
Lisandro Jimenez
Lisandro Jimenez 2014 年 6 月 6 日
コメント済み: Lisandro Jimenez 2014 年 6 月 6 日
Hello guys, I have the next problem, I want to make something like this:
A={'A1';'A2';'A3'};
them make B=somefunction(B,A); and obtain this:
B =
A1: []
A2: []
A3: []
Any idea?
  4 件のコメント
José-Luis
José-Luis 2014 年 6 月 6 日
B is a struct with fields A1, A2 and A3?
Lisandro Jimenez
Lisandro Jimenez 2014 年 6 月 6 日
That's right !

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

採用された回答

José-Luis
José-Luis 2014 年 6 月 6 日
編集済み: José-Luis 2014 年 6 月 6 日
A = {'A1';'A2';'A3'};
your_struct = cell2struct(repmat({[]},3,1),A);
Please accept an answer if it helped you.
  1 件のコメント
Lisandro Jimenez
Lisandro Jimenez 2014 年 6 月 6 日
It's better:
A = {'A1';'A2';'A3'};
your_struct = cell2struct(repmat({[]},size(A,1),1),A);

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

その他の回答 (1 件)

Lisandro Jimenez
Lisandro Jimenez 2014 年 6 月 6 日
for i=1:size(A,1)
B.(A{i})=[];
end
But this answer has a loop, and I don't know if there's another way more efficent.

カテゴリ

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