Manipulating superclass datasets within subclass methods
이전 댓글 표시
I am having some difficulty designing a subclass of dataset in Matlab (R2010b). I am experienced programming Matlab, but new to using its OOP features. My constructor seems to be fine, but when I try to build some methods that access data in the dataset, I can't seem to get it to work. Here is an example:
classdef mydataset < dataset
properties
end
methods
function [obj] = mydataset(obs,array)
% obs is N x 1 cell array of strings
% array is N x 2 double array
obj = obj@dataset({array,'Field1','Field2'},'ObsNames',obs)
end
function [val] = computeValue(obj)
col = obj.Field1;
% I get an error above regardless of how I try to access the dataset.
% e.g. col = double(obj(obs,'Field1')) also does not work.
% Some more code using col to determine val
end
end
In my method computeValue, I am trying to access the data in the dataset using dataset syntax, i.e. on the command line I could access Field1 using ".". It complains there is no method, property, or field Field1 for class mydataset. If I try the alternate syntax
col = double(obj(:,'Field1')); it complains about the size of obj, e.g. "Index exceeds matrix dimensions".
I found a workaround using subsref:
function [val] = computeValue(obj)
s.type = '.';
s.subs = 'Field1';
col = subsref(obj,s);
% Some more code using col to determine val
end
Although the workaround works, it is not very convenient and largely defeats the purpose of wanting a subclass of dataset. Is there some attribute or something simple I am missing?
I like the functionality of dataset and hope to get this, or something like it, to work so I can define custom methods extending dataset. I also tried simply making dataset a property, but the syntax got cluttered.
Thank you very much.
PS: I am not active here, so I hope cross posting from stack overflow is ok. I did not get the answer I need there so I'm trying here. I imagine some of you are active on both. Cheers.
댓글 수: 2
Geoff Hayes
2014년 6월 7일
That is strange behaviour! I don't have the dataset class definition so I can't reproduce exactly what you have above, but I did get a similar error and behaviour if I created a simple class with private properties
properties(Access=private)
Field1;
Field2;
ObsNames;
end
Due to lack of originality, I called this class dataset. I then added a simple constructor to just initialize the properties, and the public subsref function
function v = subsref(M, S)
switch S.type
case '()'
v = 'this is ok';
case '{}'
error('{} indexing not supported');
case '.'
v = builtin('subsref', M, S); % as per documentation
end
end
Then just created the mydataset as you did, with a simpler constructor and the same computeValue method. Sure enough, this method failed with the same error when it tried to access Field1 as obj.Field1. BUT, like you (?), I could access this field from the command line (in the Command Window) and get the value returned!!
So I defined a property that is private in the base class, and I can't access that property in a method of a derived class. This makes sense. BUT I can access this same field outside of the class at the command line?? If I remove the subsref method in the base class then I can't access this property at the command line either. So the behaviour of subsref is questionable - it allows access to private members even when it shouldn't.
This seems to be an issue that was discussed here http://www.mathworks.ca/matlabcentral/answers/16652-overriding-subsref-and-subsasgn-effect-on-private-properties
I'm just guessing that it is the private properties that are causing this problem/behaviour, and I don't have a solution. I think that the behaviour that you are experiencing within the class when trying to access a private data member/property makes sense. But the fact that you can access that same member by sbusref is a little surprising. (I'm using MATLAB R2014a.)
Eric
2014년 6월 7일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!