How to force MATLAB to return all values in a nested function call?

前端 未结 4 1150
太阳男子
太阳男子 2020-12-20 06:51

I find it impossible to write MATLAB code without creating a huge number of superfluous, single-use variables.

For example, suppose function foo returns

4条回答
  •  有刺的猬
    2020-12-20 07:31

    As nirvana-msu said, it is not possible to do the task without creating temporary variables. But it is possible to handle it within a function and even with varargin and varargout. Inspired by this answer on my question, you can define and use the following function:

    function varargout = redirect(F1, F2, count, list, varargin)
        output = cell(1, count);
        [output{:}] = F2(varargin{:});
        varargout = cell(1, max(nargout, 1));
        [varargout{:}] = F1(output(list));
    end
    

    For instance, in your example you can write result = redirect(@bar, @foo, 3, [1:3], 5);

提交回复
热议问题