Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to get varargout in subsref to display multiple outputs?

Subject: How to get varargout in subsref to display multiple outputs?

From: Hoi Wong

Date: 11 Mar, 2009 06:20:03

Message: 1 of 5

Normally, if you have a cell array A={1;2;3;4}, typing A{:} in the console will give

A =

    [1]
    [2]
    [3]
    [4]

but I'm trying to overload subsref for my @sparse_cell class and I want the same behavior. I have a method inside @sparse_cell/subsref that will generate multiple outputs exactly like above, and I'd like to pass that directly to the output.

I tried varargout in @sparse_cell/subsref but my sparse cell B shows only the first output if I do B{:}.

Can anybody suggest a way to truely pass multiple outputs without modifying the display behavior when no output arguments is given?

Thanks

Subject: How to get varargout in subsref to display multiple outputs?

From: James Tursa

Date: 12 Mar, 2009 00:10:51

Message: 2 of 5

"Hoi Wong" <wonghoi.ee@gmailNOSPAM.com> wrote in message <gp7l6j$hi0$1@fred.mathworks.com>...
> Normally, if you have a cell array A={1;2;3;4}, typing A{:} in the console will give
>
> A =
>
> [1]
> [2]
> [3]
> [4]
>
> but I'm trying to overload subsref for my @sparse_cell class and I want the same behavior. I have a method inside @sparse_cell/subsref that will generate multiple outputs exactly like above, and I'd like to pass that directly to the output.
>
> I tried varargout in @sparse_cell/subsref but my sparse cell B shows only the first output if I do B{:}.
>
> Can anybody suggest a way to truely pass multiple outputs without modifying the display behavior when no output arguments is given?
>
> Thanks

Please show some code. I am not sure what you mean by "... pass that directly to the output." Do you have a function that takes varargin and returns varargout, and then want to call another function from within that function that also takes a variable number of inputs and returns a variable number of outputs? If so, you can do this:

function varargout = fun1(varargin)
[varargout{1:nargout}] = fun2(varargin{:});
end

function varargout = fun2(varargin)
% etc.
end

Not sure that this is what you are asking, though.

James Tursa

Subject: How to get varargout in subsref to display multiple outputs?

From: Hoi Wong

Date: 12 Mar, 2009 08:23:52

Message: 3 of 5

"James Tursa" <aclassyguywithaknotac@hotmail.com> wrote in message <gp9jub$5ot$1@fred.mathworks.com>...
> "Hoi Wong" <wonghoi.ee@gmailNOSPAM.com> wrote in message <gp7l6j$hi0$1@fred.mathworks.com>...
> > Normally, if you have a cell array A={1;2;3;4}, typing A{:} in the console will give
> >
> > A =
> >
> > [1]
> > [2]
> > [3]
> > [4]
> >
> > but I'm trying to overload subsref for my @sparse_cell class and I want the same behavior. I have a method inside @sparse_cell/subsref that will generate multiple outputs exactly like above, and I'd like to pass that directly to the output.
> >
> > I tried varargout in @sparse_cell/subsref but my sparse cell B shows only the first output if I do B{:}.
> >
> > Can anybody suggest a way to truely pass multiple outputs without modifying the display behavior when no output arguments is given?
> >
> > Thanks
>
> Please show some code. I am not sure what you mean by "... pass that directly to the output." Do you have a function that takes varargin and returns varargout, and then want to call another function from within that function that also takes a variable number of inputs and returns a variable number of outputs? If so, you can do this:
>
> function varargout = fun1(varargin)
> [varargout{1:nargout}] = fun2(varargin{:});
> end
>
> function varargout = fun2(varargin)
> % etc.
> end
>
> Not sure that this is what you are asking, though.
>
> James Tursa

To make things simpler, please consider this:

>> A=num2cell(rand(5,1))
A =
    [0.9593]
    [0.5472]
    [0.1386]
    [0.1493]
    [0.2575]

If I want a comma separated list, I can simply do A{:}

>> A{:}
ans =
    0.9593
ans =
    0.5472
ans =
    0.1386
ans =
    0.1493
ans =
    0.2575

However, I want the output of my function to give the same comma separated list output as A{:}, but I cannot achieve it with varargout

function varargout=dummyfunction(A)
    varargout = A;
end

Everything works fine when I try to assign to an output variable, but if I don't specify any output, it assign the first output to 'ans', which is not what I wanted.

-- Hoi

Subject: How to get varargout in subsref to display multiple outputs?

From: Hoi Wong

Date: 12 Mar, 2009 08:23:52

Message: 4 of 5

"James Tursa" <aclassyguywithaknotac@hotmail.com> wrote in message <gp9jub$5ot$1@fred.mathworks.com>...
> "Hoi Wong" <wonghoi.ee@gmailNOSPAM.com> wrote in message <gp7l6j$hi0$1@fred.mathworks.com>...
> > Normally, if you have a cell array A={1;2;3;4}, typing A{:} in the console will give
> >
> > A =
> >
> > [1]
> > [2]
> > [3]
> > [4]
> >
> > but I'm trying to overload subsref for my @sparse_cell class and I want the same behavior. I have a method inside @sparse_cell/subsref that will generate multiple outputs exactly like above, and I'd like to pass that directly to the output.
> >
> > I tried varargout in @sparse_cell/subsref but my sparse cell B shows only the first output if I do B{:}.
> >
> > Can anybody suggest a way to truely pass multiple outputs without modifying the display behavior when no output arguments is given?
> >
> > Thanks
>
> Please show some code. I am not sure what you mean by "... pass that directly to the output." Do you have a function that takes varargin and returns varargout, and then want to call another function from within that function that also takes a variable number of inputs and returns a variable number of outputs? If so, you can do this:
>
> function varargout = fun1(varargin)
> [varargout{1:nargout}] = fun2(varargin{:});
> end
>
> function varargout = fun2(varargin)
> % etc.
> end
>
> Not sure that this is what you are asking, though.
>
> James Tursa

To make things simpler, please consider this:

>> A=num2cell(rand(5,1))
A =
    [0.9593]
    [0.5472]
    [0.1386]
    [0.1493]
    [0.2575]

If I want a comma separated list, I can simply do A{:}

>> A{:}
ans =
    0.9593
ans =
    0.5472
ans =
    0.1386
ans =
    0.1493
ans =
    0.2575

However, I want the output of my function to give the same comma separated list output as A{:}, but I cannot achieve it with varargout

function varargout=dummyfunction(A)
    varargout = A;
end

Everything works fine when I try to assign to an output variable, but if I don't specify any output, it assign the first output to 'ans', which is not what I wanted.

-- Hoi

Subject: How to get varargout in subsref to display multiple outputs?

From: Doug Schwarz

Date: 12 Mar, 2009 12:38:40

Message: 5 of 5

In article <gpagqo$baf$1@fred.mathworks.com>,
 "Hoi Wong" <wonghoi.ee@gmailNOSPAM.com> wrote:

[snip]

> To make things simpler, please consider this:
>
> >> A=num2cell(rand(5,1))
> A =
> [0.9593]
> [0.5472]
> [0.1386]
> [0.1493]
> [0.2575]
>
> If I want a comma separated list, I can simply do A{:}
>
> >> A{:}
> ans =
> 0.9593
> ans =
> 0.5472
> ans =
> 0.1386
> ans =
> 0.1493
> ans =
> 0.2575
>
> However, I want the output of my function to give the same comma separated
> list output as A{:}, but I cannot achieve it with varargout
>
> function varargout=dummyfunction(A)
> varargout = A;
> end
>
> Everything works fine when I try to assign to an output variable, but if I
> don't specify any output, it assign the first output to 'ans', which is not
> what I wanted.
>
> -- Hoi


You can't do it. You'll have to live with the two-line solution.

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us