[英]How to pass tuple to a Matlab function from Python
I have a Matlab function that I'm calling from a python script: 我有一个Matlab函数,我从python脚本调用:
import matlab.engine
eng = matlab.engine.start_matlab()
t = (1,2,3)
z = eng.tstFnc(t)
print z
The function tstFnc
is as follows: 函数
tstFnc
如下:
function [ z ] = tstFnc( a, b, c )
z = a + b + c
This does not work, however, as Matlab receives one input instead of three. 然而,这不起作用,因为Matlab接收一个输入而不是三个输入。 Could this be made to work?
这可以成功吗?
Note: this is a simplified case of what I want to do. 注意:这是我想要做的简化案例。 In the actual problem I have a variable number of lists that I pass into a Matlab function, which is interpreted in the Matlab function using
varargin
. 在实际问题中,我有一个可变数量的列表,我将其传递给Matlab函数,该函数使用
varargin
在Matlab函数中进行varargin
。
As notes in the comments, the arguments need to be applied instead of passed as a tuple of length 1. 作为注释中的注释,需要应用参数而不是作为长度为1的元组传递。
z = eng.tstFnc(*t)
This causes a call to tstFnc
with len(t)
arguments instead of a single tuple argument. 这会导致使用
len(t)
参数调用tstFnc
而不是单个元组参数。 Similarly you could just pass each argument individually. 同样,您可以单独传递每个参数。
z = eng.tstFnc(1, 2, 3)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.