Py学习  »  Python

从Python运行Matlab脚本:TypeError:“float”对象不可iterable

Thang Ha • 4 年前 • 141 次点击  

实际上,我在从Python调用Matlab脚本时遇到了问题。

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)

实际上,我想返回“sr”,它是一个浮点数和一个整数“state”数组,例如sr=34.31和state=[1,2,5]。函数test_func()在Matlab上运行得很好,但当我从终端(Python test_Matlab_engine.py)在Python中运行它时,收到以下错误:

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

任何人请给我答案。提前非常感谢。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/50681
 
141 次点击  
文章 [ 1 ]  |  最新文章 4 年前
Cody Gray jayce_hu
Reply   •   1 楼
Cody Gray jayce_hu    4 年前

从MATLAB到Python的结果似乎已经被切断了。如果有两个参数,则只能从MATLAB中得到第一个参数。所以,问题是如何得到两个或更多的参数。

总之,应该在Python文件中编写:

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

哪里 re 包含两个来自MATLAB的参数。

您可以在官方文档中找到更多信息: Call MATLAB Functions from Python