Constructor not working, preallocating object leaves object with no properties

function obj = ameth(t)
if nargin > 0
obj(numel(t)) = ameth;%preallocates the obj
for i=1:numel(t)
obj(i).trk = t(i);
end
end
end
This is under methods in my class definition, and is the constructor. trk is the only protected access property. When I call this method (h = ameth), I find that it returns an answer with no properties, but I am expecting to have the property trk. Where have I gone wrong?

 Réponse acceptée

per isakson
per isakson le 25 Mar 2015
Modifié(e) : per isakson le 25 Mar 2015
Works well here (R2013b)
>> t = ameth([1:4])
t =
1x4 ameth array with properties:
trk
>> t(1).trk
ans =
1
>> [t(:).trk]
ans =
1 2 3 4
>>
where
classdef ameth
properties
trk
end
methods
function obj = ameth(t)
if nargin > 0
obj(numel(t)) = ameth;%preallocates the obj
for ii = 1:numel(t)
obj(ii).trk = t(ii);
end
end
end
end
end
&nbsp
Addendum triggered by comment
>> h = ameth([1:4])
h =
1x4 ameth array with no properties.
>> h.trk
You cannot get the 'trk' property of ameth.
>>
where
properties
is replaced by
properties( Access = protected )

4 commentaires

Could it be that other methods in the class definition are interfering with my code?
per isakson
per isakson le 25 Mar 2015
Modifié(e) : per isakson le 25 Mar 2015
No, it's because trk is protected! I didn't read your question carefully enough.
Ah, I see what you mean about leaving the Access public, I changing it to public now gives what I am expecting. So just one last question (sorry, very new to oop), if the property is protected, when the method is called, we will not be notified that the object has this property?
per isakson
per isakson le 25 Mar 2015
Modifié(e) : per isakson le 25 Mar 2015
With protected the property is visible to methods of the class itself and sub-classes, but not to other classes and the "base workspace". ("class short for "instances of class".) There are better wordings in the documentation.

Connectez-vous pour commenter.

Plus de réponses (1)

Adam
Adam le 25 Mar 2015
Modifié(e) : Adam le 25 Mar 2015
Are you sure it has no properties? If it is a protected property you have then you will not be able to see it when you display the object on command line, you will only see it inside the object (or inside an object of a derived class), e.g. in the breakpoint of a class function and even then it will not display in command window variables.
I often make my variables public while I am still working on developing a class just so I can see them easily, then I make them private or protected once I am satisfied things are working.

2 commentaires

Yes, you are right, I expected in the command line to see that I had the property trk, but as it was protected it wasn't visible. It is my understanding that it still does exist as a property
Yes, it will exist and be accessible within the class and derived classes. If you really want you can over-ride the display functions in a class to make them show private and protected properties, but I can never be bothered.

Connectez-vous pour commenter.

Catégories

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by