Oct2Py仅返回第一个输出参数 [英] Oct2Py only returning the first output argument

查看:117
本文介绍了Oct2Py仅返回第一个输出参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Oct2Py,以便在我的Python代码中使用一些M文件.假设我有一个简单的Matlab函数:

I'm using Oct2Py in order to use some M-files in my Python code. Let's say that I have this simple Matlab function :

function [a, b] = toto(c);
    a = c;
    b = c + 1;
end

如果我在Octave中调用它,显然会发生:

What happens if I call it in Octave is obviously :

>> [x,y] = toto(3)
x = 3
y = 4

现在,如果我使用oct2py在Python中调用它:

Now if I call it in Python, using oct2py :

from oct2py import octave
my_dir = "D:\\My_Dir"
octave.addpath(my_dir)
a,b = octave.toto(3)

这将返回:

TypeError:"int"对象不可迭代

TypeError: 'int' object is not iterable

似乎octave.toto(n)仅返回第一个值,当我期望两个值时...有人可以向我解释我应该做什么吗?谢谢

It seems like octave.toto(n) only returns the first value, when I'd expect two... Can anyone explain to me what I should be doing ? Thanks

推荐答案

在旧版本的Oct2Py(3.x及更早版本)中,输出参数的数量是通过Python中的调用推断出来的,因此,如果您需要多个输出,您只需请求两个输出

In older versions of Oct2Py (3.x and older), the number of output arguments was inferred from the call within Python, so if you wanted multiple outputs, you would simply request both outputs

a, b = octave.toto(3)

但是,从4.0版开始 a>现在,您需要在函数调用中使用nout kwarg来显式指定所需数量的输出参数

However, as of version 4.0 you now need to use the nout kwarg to your function call to explicitly specify the desired number of output arguments

a, b = octave.toto(3, nout=2)

从4.0发行说明中

删除了对八度函数调用的推断的nout;如果不是,则必须明确给出它.1.旧的行为太令人惊讶了,并且依赖于CPython解释器的内部逻辑.

Removed inferred nout for Octave function calls; it must be explicitly given if not 1. The old behavior was too surprising and relied on internal logic of the CPython interpreter.

这篇关于Oct2Py仅返回第一个输出参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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