Easiest Way to Assign Graphics Objects to a 1-by-n Struct?

1 回表示 (過去 30 日間)
Rightia Rollmann
Rightia Rollmann 2017 年 3 月 6 日
コメント済み: Adam 2017 年 3 月 6 日
How to assign graphics objects to a 1-by-5 struct field? Is there an easier way than using a for loop and assign the values elementwise?
When there is more than one row, it works perfectly and I get 10 rectangles:
Rectangles1(2, 5).r = rectangle();
But when I run the code below with only one row, MATLAB only creates one rectangle, instead of 5 rectangles?
Rectangles2(1, 5).r = rectangle();
  2 件のコメント
Walter Roberson
Walter Roberson 2017 年 3 月 6 日
No,
Rectangles1(2, 5).r = rectangle();
only assigns 1 rectangle, to element (2,5) . The "r" fields for the other structure members will be empty.
Adam
Adam 2017 年 3 月 6 日
A for loop seems like the best way. rectangle does not support vectorised creation and if you try to get fancy with something like:
[ Rectangles2(1, 1:5).r ] = deal( rectangle() );
you get a rectangle in each of your structs, but it is a reference to the same rectangle in all of them which I assume is not what you want.
To create 5 distinct rectangles you will need 5 calls to rectangle( ) by some method or other.

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

採用された回答

Walter Roberson
Walter Roberson 2017 年 3 月 6 日
template = ones(2,5); %shape matters, content does not
Rectangles2 = cell2mat( arrayfun( @(r) struct('r', rectangle()), template, 'Uniform', 0) );
This will, however, overwrite the entire contents of Rectangles2
  2 件のコメント
Rightia Rollmann
Rightia Rollmann 2017 年 3 月 6 日
編集済み: Rightia Rollmann 2017 年 3 月 6 日
Thank you!
What does
'Uniform', 0
do?
Adam
Adam 2017 年 3 月 6 日
Try it without and you'll get an error message telling you. Basically it means a regular array of whatever the output is is not supported by arrayfun though so you need the 'catch-all' option of dumping the outputs into a cell array.

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

その他の回答 (0 件)

カテゴリ

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