Run same function for different objects from same class

5 ビュー (過去 30 日間)
Dario Walter
Dario Walter 2019 年 11 月 1 日
回答済み: Walter Roberson 2019 年 11 月 4 日
Hey guys,
I am still a beginner in OOP. Hope, that the question is not too stupid ;).
Let's assume geMode1,geMode2,geMode3,geMode4 and geMode5 are objects of the Mode Class. How is it possible to execute the testfct function in one line of code (preferably without for-loop) so that it is applied to all objects?
One example could be
function obj = testfct(obj,color)
obj.color = color;
end
I want all objects to have the same color. To define obj = {geMode1,geMode2....} does not really work.
Thanks for your help :)
  1 件のコメント
Daniel M
Daniel M 2019 年 11 月 1 日
Look into structfun()

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

回答 (2 件)

Harsha Priya Daggubati
Harsha Priya Daggubati 2019 年 11 月 4 日
Hi,
You can try initializing color property of the object in the constructor of the Model class. Refer to the following link for more details:
Hope this helps!

Walter Roberson
Walter Roberson 2019 年 11 月 4 日
arrayfun(@(obj) testfct(obj, SomeColor), [geMode1, geMode2, geMode3, geMode4, geMode5])
Provided that they are handle class. If not then modifying them in a compact way becomes more difficult.

カテゴリ

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

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by