MATLAB: Manipulating superclass datasets within subclass methods

classdatasetoopsubref

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.

Best Answer

It appears that the dataset superclass has a subsref method defined, and hence is inherited by your subclass mydataset. When the class possesses a subsref method, indexing behavior outside class methods (e.g., at the command line) is different from inside them.
Inside the classdef, dot indexing never calls the class' subsref method. Instead, it is always interpreted as an attempt to access properties and methods of the class. In the first version of the mydataset classdef that you posted, Field1 and Field2 are not properties of the class (they are just data stored in some protected superclass property), so your attempt to access them by dot-indexing inside a class method fails. To get at the data, you must do so indirectly through explicit calls to subsref (as in your workaround), or whatever other indirect access methods the dataset class might have defined.
Outside the class definition, indexing expressions always call the class' subsref method, in this case subsref@dataset or subsref@mydataset, if you chose to overload it, and do whatever subsref dictates.
It's not entirely clear to me what indexing behavior you are trying to achieve, and where you want to achieve it. "Where" is important because indexing behavior cannot be the same both inside and outside the classdef. However, be aware that you are always free to invoke class methods in functional form, i.e.,
classmethod(obj,arg1,arg2,...)
will always work whether inside the class definition or elsewhere, whereas outside the class definition
obj.classmethod(arg1,arg2)
will invoke subsref@mydataset.