Constructor not working, preallocating object leaves object with no properties

4 views (last 30 days)
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?

Accepted Answer

per isakson
per isakson on 25 Mar 2015
Edited: per isakson on 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 Comments
Michael
Michael on 25 Mar 2015
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 on 25 Mar 2015
Edited: per isakson on 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.

Sign in to comment.

More Answers (1)

Adam
Adam on 25 Mar 2015
Edited: Adam on 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 Comments
Michael
Michael on 25 Mar 2015
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
Adam
Adam on 25 Mar 2015
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.

Sign in to comment.

Categories

Find more on Create System Objects in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!