Setting the properties of an Object Array

10 ビュー (過去 30 日間)
Nicholas Dinsmore
Nicholas Dinsmore 2012 年 10 月 1 日
The documentation and experimentation make it clear that I can do this to get the property values of an array of objects:
Array_of_property_values=[Object_Array(:).Property];
The question is how can I do the reverse without major use of subsasgn. For example I want to be able to do something like the following
[Object_Array(:).Property]=Array_of_property_values_to_assign;
I just can't seem to make that work. I have tried many different permutations but nothing seems to work. Does any on have any thoughts on how to make that work.
I have tried the following with no success:
[Object_Array(1:2).Property]=[10,20];
% suggestion from Stackoverflow
[Object_Array(:).Property]=deal(Array_of_property_values_to_assign);

採用された回答

Matt J
Matt J 2012 年 10 月 1 日
args=num2cell(Array_of_property_values_to_assign);
[Object_Array(:).Property]=deal(args{:});
  3 件のコメント
Matt J
Matt J 2012 年 10 月 1 日
編集済み: Matt J 2012 年 10 月 1 日
If Array_of_property_values_to_assign is already a cell array, then you can just do
[Object_Array(:).Property]=deal(Array_of_property_values_to_assign{:});
Otherwise, no, there is no way to do it in 1 line, except for fancy subsasgn methods.
per isakson
per isakson 2012 年 10 月 3 日
- or write an appropriate version of deal.

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

その他の回答 (1 件)

Grzegorz Lippe
Grzegorz Lippe 2013 年 6 月 11 日
Hello,
this isn't working for me and I can't figure out what I'm doing wrong:
>> a = num2cell(1:3)
a =
[1] [2] [3]
>>[p(:).h_fP_star] = deal(a{:});
>>[p.h_fP_star]
ans =
3 3 3
Could anybody help?
  1 件のコメント
Matt J
Matt J 2013 年 6 月 11 日
Type
>>methods(p)
and show us the result.

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

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by