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?

 채택된 답변

per isakson
per isakson 2015년 3월 25일
편집: per isakson 2015년 3월 25일
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

Could it be that other methods in the class definition are interfering with my code?
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?
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Adam
Adam 2015년 3월 25일
편집: Adam 2015년 3월 25일
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

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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Extend Unit Testing Framework에 대해 자세히 알아보기

제품

질문:

2015년 3월 25일

편집:

2015년 3월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by