[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[cell_array{:}] = foo () syntax and semantics
From: |
John W. Eaton |
Subject: |
[cell_array{:}] = foo () syntax and semantics |
Date: |
Fri, 17 Sep 2004 09:26:45 -0400 |
Octave currently does not handle things like
[cell_array{:}] = foo ()
I've given this some thought in the past, and even started working on
it once, but had to give up when it became fairly complicated and I
ran out of time. I plan to work on this again soon, but before I
start, I'd like to get some feedback.
For functions that are wrappers around others (like inline or
anonymous function handles) it would be swell if we could write
something as simple as (for example)
function varargout = foo (varargin)
[varargout{:}] = svd (varargin{:});
endfunction
and have it work in all cases:
foo ([1,2;3,4]);
sigma = foo ([1,2;3,4]);
[u, s, v] = foo ([1,2;3,4]);
Can someone tell me whether this will work in Matlab R14? In R13 it
fails for the nargout == 0 case with an error about the subscript
count of the cell array being zero, and also for the nargout > 0 cases
because varargout is not preallocated automatically based on the value
of nargout, so you have to write the wrapper as
function varargout = foo (varargin)
if (nargout == 0)
svd (varargin{:});
else
varargout = cell (nargout, 1);
[varargout{:}] = svd (varargin{:});
end
But even this is not quite right as ans (in the nargout == 0 case) is
not propagated to varargout. So really, you have to write
function varargout = foo (varargin)
if (nargout == 0)
svd (varargin{:});
varagout = ans;
else
varargout = cell (nargout, 1);
[varargout{:}] = svd (varargin{:});
end
Hmm. Although in the svd case, the behavior is identical for the
nargout == 0 and nargout == 1 cases, that may not always be the case.
So we can't write
function varargout = foo (varargin)
n = nargout;
if (nargout == 0)
n = 1;
end
varargout = cell (n, 1);
[varargout{:}] = svd (varargin{:});
But, *could* we make
function varargout = foo (varargin)
[varargout{:}] = svd (varargin{:});
endfunction
do this automatically? I think we would only have to do the
following (in addition to making the [x{:}] = foo (...) syntax work to
begin with):
* Preallocate varargout based on the value of nargout.
* Allow
retval = {};
[retval{:}] = fcn (...);
to work such that nargout is set to 0 for fcn, but if fcn returns
any values, retval is reallocated to a one-element cell array to
hold toe first returned value.
Would these special cases cause trouble anywhere else (i.e., change
the behavior of currently valid code)?
jwe
- [cell_array{:}] = foo () syntax and semantics,
John W. Eaton <=