【问题标题】:Run Matlab script from Python: TypeError: 'float' object is not iterable从 Python 运行 Matlab 脚本:TypeError: 'float' object is not iterable
【发布时间】:2019-06-20 23:27:47
【问题描述】:

其实我从 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”,它是一个浮点数和一个整数“状态”数组,例如sr = 34.31 和状态 = [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

任何人请给我解决方案。非常感谢您。

【问题讨论】:

  • 您也可以编辑以共享 MATLAB 代码吗?当我看不到引发异常的函数时,很难为您提供帮助

标签: python-2.7 matlab typeerror matlab-engine


【解决方案1】:

从MATLAB到Python的结果好像被截断了。如果你有两个参数,你只能得到一个来自 MATLAB 的第一个参数。所以,问题是如何获取两个或多个参数。

总之,你应该把这个写在你的 Python 文件中:

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

其中re 包含两个来自 MATLAB 的参数。

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

【讨论】:

    猜你喜欢
    • 2021-08-08
    • 1970-01-01
    • 2018-07-28
    • 2022-08-23
    • 1970-01-01
    • 1970-01-01
    • 2020-11-07
    • 1970-01-01
    • 2016-03-25
    相关资源
    最近更新 更多