3

dataset allows us to do:

x = rand(10, 1); 
y = rand(10, 1);
d = dataset(x, y);

d will have 2 variables with name 'x' and 'y' and content x and y - variable names are obtained from the workspace. The dataset() call above is equivalent to:

d = dataset({'x', x}, {'y', y});

when the names are specified.

Now if I have a subclass of dataset:

classdef mydataset < dataset
properties
end

methods
    function spec = mydataset(varargin)
        spec = spec@dataset(varargin{:});    
        % Add some more things to this subclass because that's the reason I need a subclass
    end 
end 
end    

The problem is, if I call:

d = mydataset(x);

d will have the variable x but the name is just 'var1'. The workspace name 'x' is not recognized. Unless I call:

d = mydataset({'x', x});

I will not get the same effect.

Any solution?

Note that I do not want to lose other argument parsing abilities of dataset(). It can process really complicated arguments, and I do want to preserve that.

http://www.mathworks.com/help/toolbox/stats/dataset.html

A = dataset(varspec,'ParamName',Value)
A = dataset('File',filename,'ParamName',Value)
A = dataset('XLSFile',filename,'ParamName',Value)
A = dataset('XPTFile',xptfilename,'ParamName',Value)

The example in this question with mydataset(x) is the a simple and commonly encountered situation that mydataset() can't pass things to dataset() and obtain the same results. Thus it's an important situation. But to do just that and lose other capabilities of dataset() is not worth it.

cinny
  • 2,292
  • 3
  • 18
  • 23

1 Answers1

1

One option is to capture the argument names yourself and build a cell that you then pass in to the dataset constructor, i.e. you build a cell that looks like

{{Var1 VarName1}, {Var2 VarName2}, ...}

A quick and dirty solution:

classdef mydataset < dataset

    properties
    end

    methods

        function self = mydataset(varargin)

            for k = 1:nargin
                args{k} = {varargin{k}, inputname(k)};
            end

            self = self@dataset(args{:});

        end

    end

end

Now if I call it:

>> x=1;
>> y=2;
>> mydataset(x,y)
ans = 
    x    y
    1    2

Of course, you've now lost the ability to call mydataset with the {val, valname},... syntax, but maybe that's worth giving up. If you also wanted to be able to do that, you would need to write a conditional that checks the format of your input first, and builds args differently depending on the input format.

Note that you can't do the obvious thing and put your calls to the superclass constructor inside two branches of an if statement. In Matlab, calls to the superclass have to be at the top level (i.e. you can't put them inside loops or conditionals).

Chris Taylor
  • 46,912
  • 15
  • 110
  • 154
  • I'm aware of this method. But I do not want to do the parsing myself because I'll then lose all the other dataset() capabilities (of course unless I can replicate all of those abilities). If you look up on what kind of arguments dataset() can take, the above will fail in every way but the most simple case. – cinny Mar 23 '12 at 18:16
  • The code for `dataset` is open source -- why don't you base your parsing code on that? You're always going to have the problem that when your variables are passed to the constructor of `mydataset`, information about their original names is lost - except if you use `inputname` to recover them. That's the way that `dataset` is able to capture their names. If you want to have a function in between your workspace and the call to dataset, you're going to have to get the function names yourself, which will involve writing a parser essentially as complicated as the one in `dataset`'s constructor. – Chris Taylor Mar 23 '12 at 19:34
  • The "real" answer is that `dataset`'s ability to capture the workspace names of its inputs is a bit of a hack. In a programming language with nice properties (e.g. lexical scoping) this wouldn't be allowed. You could make a good argument that the best thing to do is not use these capabilities in the first place. I agree that they can be convenient though! – Chris Taylor Mar 23 '12 at 19:36
  • I do agree with every point. I guess I was just throwing it out there just in case there's an 'easy' hack. I was thinking of something like this http://stackoverflow.com/questions/9817061/matlab-how-to-retrieve-the-exact-parameter-list-of-the-function-call-as-a-stri as a solution. Get the string, then do an eval with dataset(). It's sort of a hacking way and I'm not even sure if it works or if it's worth it. – cinny Mar 23 '12 at 19:52
  • I tried to google but have not found the dataset() source code. If you know where it is and can point it out to me it'd be much appreciated. – cinny Mar 23 '12 at 19:53
  • You should be able to type `edit dataset` at the Matlab prompt to see it. It works for me on Matlab 2008b and 2009a. – Chris Taylor Mar 24 '12 at 00:33