如何获得所有输出(MatLab)? [英] How to get all outputs (MatLab)?

查看:62
本文介绍了如何获得所有输出(MatLab)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个函数可以给出未知数量的输出参数(它取决于输入,因此在循环中会发生变化).如何获得所有这些?

Suppose I have a function that gives out unknown number of output arguments (it depends on input,thus change through the loops). How to get all of them?

nargout 没有帮助,因为该函数使用 varargout (结果为-1)

nargout doesn't help as the function uses varargout (the result is -1)

当然我不能重写该函数,否则问题不会出现:-)

And of course I can't rewrite the function, otherwise the question wouldn't arise :- )

推荐答案

感谢所有参与讨论的人.总结起来,似乎该问题没有通用的解决方案,因为MatLab本身会在函数调用之前估算所需输出的数量,以在内部使用该函数.可以指出三种情况:

Well, thanks to all partisipated in discussion. Summing up, it seems the problem has no general solution, because MatLab itself estimates the number of desired outputs before the function call to use inside it. Three cases can be pointed out though:

1)函数没有定义为 varargout ,因此 nOut = nargout(@fcn)返回正数.

1) The funcrion doesn't have varargout in definition, thus nOut=nargout(@fcn) returns positive number.

然后 nOut 是实际的输出数量,我们可以使用单元格数组和列列表技巧.

Then nOut is an actual number of outputs and we can use a cell array and a column list trick.

X=cell(1,nOut);
[X{:}]=fcn(inputs);

2)该功能定义的定义为 varargout ,因此 nOut = nargout(@fcn)返回负数.但是,可以找到与输入的一些相关性(例如 length(varargin)= length(varargout)).

2) The funcrion has varargout in definition, thus nOut=nargout(@fcn) returns negative number. However some correlation with inputs can be found (like length(varargin)=length(varargout)).

然后我们可以从 inputs 计算出结果 nOut 并执行上述列列表技巧.

Then we can calculate the resulting nOut from inputs and perform the above column list trick.

3)您认识 fcn 开发人员.

3) You know the fcn developer.

请他帮忙.例如,使函数的输出成为一个单元格数组.

Ask him fot assistance. For example to make the function's output to be a cell array.

这篇关于如何获得所有输出(MatLab)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆