How to create classes in for loop?

Hello
i would like to create objects in a for loop, where it get his number through an input of his number. In another for loop the object should be called with the help of the number and the properties should be set. How it can be done?
Thank you in advance!

Answers (1)

Just use a regular for loop with indexing. E.g.,
for k=1:n
x(k) = myclass(whatever initialization is appropriate goes here);
end
Then you can set properties downstream. E.g.,
x(1).prop1 = something;
x(2).prop2 = something_else;
etc.

12 Comments

As i know this would work with public properties, but how it can be done with private properties?
I'm not sure yet what the issue is. Isn't there a class method you can use to set the private property?
As i know you can not access the private properties directly, only with the help of methods (functions)!
Either you should set the private properties at the time of construction, or else you should provide a public method that sets the private properties as required.
This is not necessarily a direct "set" routine about a property that the user knows about. For example, you might have a method named RoomArea() that happens to also increment a private property that is a usage counter. As long as the public methods somehow manage the private properties, it should be okay.
Could you please make an example?
Are you asking for an example at the user level? The user should just be calling the constructor and any public methods, and nothing the user does has to have an obvious relationship to the private variables.
For example, there could be a private state that is holding an ActiveX object for communicating with (say) a fingerprint scanner, and the public methods would be responsible for triggering private calls that maintain that state.
If you are asking for examples of writing class methods that manage private properties then see for example https://www.mathworks.com/matlabcentral/answers/351614-how-to-set-a-private-property
I know how to access and change the private properties in an object.
For example there is an object house. In a for loop should be constructed 10 different houses with different size, color, shape and so on. The size, color and shape are the private properties of the house. How can i change this properties after the construction so that the properties are implemented to the right house?
"The size, color and shape are the private properties of the house"
Are they? That once the user has somehow set them (through some chain of calls), it is intentional that the user will not be able to query the values of those properties?
Private properties should be used for implementation details, not for properties that are intended to be read only. For properties that are intended to be read only, you can set SetAccess to be private independently of GetAccess
I know, but how can this properties can be set with the method SetAccess in a loop?
Guillaume
Guillaume on 12 Jul 2018
Edited: Guillaume on 12 Jul 2018
Michael, your question is a bit bizarre. The whole point of private properties (which in matlab means that both SetAccess and GetAccess are set to private) is that they're not meant for the caller to set them. If you do want the caller to be able to change their values, then there's no point in them being private.
Can you maybe describe your use case in details, so we understand what you're trying to achieve.
How can this be done for a gprfit class? For example, this is not possible:
for i = 1:10
gpr(i) = fitrgp(X,Xdot(:,i));
end
Thanks,
If the class in question doesn't allow users to store them in an array, put them in a cell array instead. For instance, you can't put function handles in an array but you can put them in a cell array instead.
c = cell(1, 3);
for k = 1:3
c{k} = @(x) x.^k;
end
c{3}(7) % 7^3 = 343
ans = 343
% this code WON'T work
a = @(x) x.^1;
a(2) = @(x) x.^2;
Nonscalar arrays of function handles are not allowed; use cell arrays instead.

Sign in to comment.

Categories

Asked:

on 11 Jul 2018

Commented:

on 12 Nov 2020

Community Treasure Hunt

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

Start Hunting!