1

I called a Matlab function from Python, the function has 3 outputs. Python gives ValueError: not enough values to unpack (expected 3, got 2). Here's the Matlab function testCalledByPython:

function [otpt1, otpt2, otpt3] = testCalledByPython(inpt)
otpt1 = rand(inpt, inpt);
otpt2 = magic(inpt);
otpt3 = zeros(inpt, inpt);

This is the Python script to call the above function:

#!/usr/bin/env python3
import matlab.engine
eng = matlab.engine.start_matlab()
otpt1, otpt2, otpt3 = eng.testCalledByPython(2)

If I replace the last line by otpt1, otpt2 = eng.testCalledByPython(2), it runs, but the outputs are:

otpt1, otpt2
Out[5]: 
(matlab.double([0.8147236863931789,0.12698681629350606]),
 matlab.double([0.9057919370756192,0.9133758561390194]))

Obviously these are wrong outputs. How can I solve it?

2 Answers 2

2

I found the solution myself. It's actually explained in Matlab Documentation: https://uk.mathworks.com/help/matlab/matlab_external/call-matlab-functions-from-python.html

So knowing there are 3 outputs, the way to do it is changing last line of Python script to:

otpt1, otpt2, otpt3 = eng.testCalledByPython(2, nargout = 3)

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

Comments

0

I also had the similar problem, however I was using the runtime method without API engine.

So knowing that I required/expected x outputs from the function, I added the argument of nargout = x inside the function that I wanted to use or get output from.

Comments

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.