Use variable number of output while assigning to varargout

I'm making a generic subsref for my classA, which has an attribute attrA that is a classB instance. So far, it's working, it lets me do things like

x = objA.attr1.methB(), 

which was what I was trying to do to do in the first place.

function this = Class1(varargin)
    this.attrA = ClassB()
    this = class(this,'ClassA')

function this = ClassB()
    this.AttrB1 = 'valueB1'
    this.AttrB2 = 'valueB2'

function out = methB
    out = this.AttrB2

The problem I stumbled upon is this: when the call to a method is executed in my subsref, I do it like this (detecting that it's a method etc is done before):

methName = index(1).subs;                    
args = index(2).subs;
if iscell(args)                    
    varargout = {feval(methName,this,args{:})};            
else
    varargout = {feval(methName,this,args)};
end %end if iscell

The problem is that when the methName method supports variable number of outputs, this varargout is not equivalent to [x,y,...] (the number of outputs should be assigned in the call to subsref, so methName always returns a single output, which is not always what I want (almost, but not always).

How would I let methName know how many outputs I want? (I don't want to have to pass N as a parameter).

I'm thinking something like creating a string str='[out1,out2,out3...]' and then doing something like

eval([ 
str ...
'= {feval(methName,this,args{:})};'...
])

But I keep thinking there must be a more elegant way of doing it.

Answers


If the number of outputs you expect is dependent on the output arguments requested from your subsref, you can simply use nargout like this:

[varargout{1:nargout}] = feval(methName,this,args{:});

This solution worked, but I needed to add a little something for single outputs:

if iscell(args)
    [argout{:}] = feval(methName,this,args{:});            
else
    [argout{:}]= {feval(methName,this,args)};
end %end if iscell`
if isSingleCell(argout) && iscell(argout{1})`
    v = argout{1};
    argout{1}=v{1};
end

I'm not sure, but it may be that the last bit was only necessary to fix something else ( I had to fix a lot of other things to make this work). I'll get back to it when I finish what I was trying to do with this class


Need Your Help

Handle leaking in WinAPI CreateFile?

c++ winapi cd-burning

CreateFile allocates 2(!!) handles and CloseHandle closes only one handle when trying to get the low-level access to cd-rom device. OS Windows XP SP3, 5 of 7 tested computers works the same.

How do you do modulo or remainder in Erlang?

erlang modulo

I'm brand new to Erlang. How do you do modulo (get the remainder of a division)? It's % in most C-like languages, but that designates a comment in Erlang.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.