0

Actually I have a problem when calling a Matlab script from Python.

import matlab.engine

import os
import random
import numpy as np

a=[str(random.randint(1,3)) for _ in range(3)]
print(a)
eng=matlab.engine.start_matlab()
eng.cd("/Users/dha/Documents/MATLAB/test-matlab/",nargout=0)
sr, state=eng.test_func()
print(sr)
print(state)

In fact I want to return "sr" which is a float and an array of integer "state", e.g. sr = 34.31 and state = [1,2,5]. The function test_func() work well on Matlab, but when I run this in Python from terminal (python test_matlab_engine.py) I received the following error:

Traceback (most recent call last):
  File "test_matlab_engine.py", line 10, in <module>
    sr, state=eng.mabuc_drl(a)
TypeError: 'float' object is not iterable

Anyone please give me the solution. Thank you so much in advance.

Thang Ha
  • 21
  • 1
  • 2
  • Can you edit to share the MATLAB code too? It is hard to help you when I can't see the function the raises the exception – Mike C Jan 29 '19 at 14:19

1 Answers1

0

It seems that the result from MATLAB to Python has been cut off. If you have two parameters, you only get one which is the first parameter from the MATLAB. So, the question is how to get two or more parameters.

In a word, you should write this in your Python file:

re = eng.your_function_name(parameter1, parameter2, nargout=2)

where re contains two parameters which come from MATLAB.

You can find more information in the official documentation: Call MATLAB Functions from Python

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
jayce_hu
  • 1
  • 1