New class object every iteration

5 ビュー (過去 30 日間)
Miroslav Jiránek
Miroslav Jiránek 2020 年 5 月 2 日
回答済み: per isakson 2020 年 5 月 2 日
Hello. I'm trying to create new class object in every single iteration. I've founded a lot of topics how to do this with matrixes, cell arrays and many other elements, but not with object class. The idea is to creeate something like this
for i = 1:1:10
new_obj_{i}=class_name
end
after end of the for cycle, there should be 10 objects of class_name, named: new_obj_1, new_obj_2, new_obj_3 and so on..
Is there any way how to do it?

採用された回答

per isakson
per isakson 2020 年 5 月 2 日
In most cases it's better to create an array of objects. The documentations provides one way. See Construct Object Arrays. What are the pro and cons in your case?

その他の回答 (1 件)

Ameer Hamza
Ameer Hamza 2020 年 5 月 2 日
Do you want to create your variable named dynamically like new_obj_1, new_obj_2, ...? This is not a recommended practice. Read here in detail: https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval. It is much better to use a simple array or a cell array as you are doing right now.
You can also create an array with for-loop like this
new_obj_ = repmat(class_name, 1, 10)

カテゴリ

Help Center および File ExchangeConstruct and Work with Object Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by