How to create classes in for loop?
Show older comments
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)
James Tursa
on 11 Jul 2018
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
Michael Simonovski
on 11 Jul 2018
James Tursa
on 11 Jul 2018
I'm not sure yet what the issue is. Isn't there a class method you can use to set the private property?
Michael Simonovski
on 11 Jul 2018
Edited: Michael Simonovski
on 11 Jul 2018
Walter Roberson
on 11 Jul 2018
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.
Michael Simonovski
on 11 Jul 2018
Walter Roberson
on 11 Jul 2018
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
Michael Simonovski
on 11 Jul 2018
Walter Roberson
on 12 Jul 2018
"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
Michael Simonovski
on 12 Jul 2018
Edited: Michael Simonovski
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.
Matthew Osborne
on 12 Nov 2020
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
% this code WON'T work
a = @(x) x.^1;
a(2) = @(x) x.^2;
Categories
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!