Assign value to array structure

16 ビュー (過去 30 日間)
Peter Borda
Peter Borda 2015 年 6 月 19 日
編集済み: Stephen23 2015 年 6 月 19 日
Hi Everybody! I have an array of let's say 100 elements, each element is the same structure with like 10 fields. What I would like to do is to generate an array - cell or numeric - of the same length as the structure array and assign each elements to a specific field of the individual structures. Here is the solution with for loop, I'd like to make the same without looping:
BaseArray = struct('field_1',[],'field_c2',[],'field__3',[]); % struct to be used
StructArray(100) = BaseArray; % initialize
ValuesToAssign = rand(100);
for k = 1:length(StructArray)
StructArray(k).field_1 = ValuesToAssign(k);
end
Any advice?

採用された回答

Titus Edelhofer
Titus Edelhofer 2015 年 6 月 19 日
Hi,
this should work:
valuesCell = num2cell(ValuesToAssign);
[StructArray.field_1] = valuesCell{:};
Titus
  2 件のコメント
Peter Borda
Peter Borda 2015 年 6 月 19 日
Great, thanks! Drastic speed improvement as expected!
Stephen23
Stephen23 2015 年 6 月 19 日
編集済み: Stephen23 2015 年 6 月 19 日

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

その他の回答 (0 件)

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by