3

I have a Matlab function that I'm calling from a python script:

import matlab.engine

eng = matlab.engine.start_matlab()
t = (1,2,3)
z = eng.tstFnc(t)
print z

The function tstFnc is as follows:

function [ z ] = tstFnc( a, b, c )
z = a + b + c

This does not work, however, as Matlab receives one input instead of three. 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.

3
  • 1
    Try z = eng.tstFnc(*t) to apply the arguments instead of passing them as a tuple Commented Sep 2, 2015 at 17:05
  • @Pyrce I can't believe that worked :D Thanks! Commented Sep 2, 2015 at 17:06
  • Great! I'll add it as the answer Commented Sep 2, 2015 at 17:07

1 Answer 1

3

As notes in the comments, the arguments need to be applied instead of passed as a tuple of length 1.

z = eng.tstFnc(*t)

This causes a call to tstFnc with len(t) arguments instead of a single tuple argument. Similarly you could just pass each argument individually.

z = eng.tstFnc(1, 2, 3)

Sign up to request clarification or add additional context in comments.

3 Comments

I should add that t = (2,3) and z = eng.tstFnc(1, *t) works too, in case anyone is curious.
@sodiumnitrate - Yes, that's a standard Pythonic way of splitting up the tuple. BTW, I've already +1ed your question and this answer.
@rayryeng Yeah, ok, I didn't know :) Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.