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 件のコメント

Michael
Michael 2015 年 3 月 25 日
Could it be that other methods in the class definition are interfering with my code?
per isakson
per isakson 2015 年 3 月 25 日
編集済み: per isakson 2015 年 3 月 25 日
No, it's because trk is protected! I didn't read your question carefully enough.
Michael
Michael 2015 年 3 月 25 日
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 2015 年 3 月 25 日
編集済み: per isakson 2015 年 3 月 25 日
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 日

1 投票

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 件のコメント

Michael
Michael 2015 年 3 月 25 日
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 2015 年 3 月 25 日
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 ExchangeExtend 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