0

I am trying to call a Matlab function in python via matlab.engine. The function takes one input (a number corresponding to filenum) and ouputs 4 matlab arrays. In the python script I try taking these 4 variables and converting them to a numpy array, however I get an error:

ValueError: too many values to unpack (expected 4)

The original Matlab function line reads as:

function [out1,out2,out3,out4] = myfunc(filenum)

I have tried alternate methods too see why this was happening. Interestingly, if I only store the output of the matlab function in one variable the code works fine, but the variable value is not the one I desire.

Here is all relevant code (other than the matlab function itself):

import matlab.engine
import numpy as np

eng = matlab.engine.start_matlab()

out1,out2,out3,out4 = eng.myfunc(101)

final1 = np.asarray(out1)
final2 = np.asarray(out2)
final3 = np.asarray(out3)
final4 = np.asarray(out4)


Before the final1... code block can even run I get the error on the line where I try calling the function.

Also I am positive the Matlab function works and it does output 4 arrays.

1 Answer 1

1

The documentation says:

If you know that the function can return multiple arguments, use the nargout argument to specify the number of output arguments.

And then it gives an example:

t = eng.gcd(100.0,80.0,nargout=3)
print(t)

(20.0, 1.0, -1.0)

Thus, you probably need to do

out1,out2,out3,out4 = eng.myfunc(101,nargout=4)
Sign up to request clarification or add additional context in comments.

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.