如何通过多输出返回功能? [英] How to pass a multi-output return to a function?

查看:73
本文介绍了如何通过多输出返回功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个多输出代码

If I have a multi-output code

function [a b] = foo() 
  a = 1;
  b = 2;
end

我们可以直接将foo()的结果传递给另一个函数吗

Can we pass foo()'s result to another function directly,

function test(a)
   a
end

test(foo());

仅打印1.

在测试中,我不确定如何破坏其论点.它不是数组. 我们必须做吗?

I am not sure, in test, how we can destruct its argument. It is not an array. Do we have to do?

[x, y] = foo();
test(x, y);

推荐答案

不,没有其他方法.

此方法的一种变体是使用单元格数组和逗号分隔的列表.示例:

One variation of this is using a cell arrays and comma-separated lists. Example:

C = cell(1,2);
[C{:}] = foo();
bar(C{:})

您可以将上述代码用于任意数量的输入/输出参数,只需将上面的2替换为实际的数字即可.

you can reuse the above code for any number of input/output arguments, just replace 2 above with the actual number.

C = cell(1,n);
[C{:}] = foo();  % equivalent to [C{1},C{2},...,C{n}] = foo()
bar(C{:})        % equivalent to bar(C{1},C{2},...,C{n})

这篇关于如何通过多输出返回功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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