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