python - 使用 .mat 文件内容作为 Python 的参数调用 Matlab 函数
问题描述
我正在构建一个软件,该软件的一部分需要读取 .mat 文件并将内容作为参数提供给 matlab 函数。问题是我猜我的数据结构不好。
事实上,我调用的 Matlab 结构handles
与我handles
在 Python 中的数据结构不同(转换后)
当我从 python 加载 .mat 文件时scipy.io.loadmat()
,我有这 4 个键:
所以handles.simulation
里面应该有这个内容:
因此matlab.engine
,在我的情况下,我使用调用 matlab 函数来调用该函数,VS_Parametrise
并且当我传递 my handles
which 是一个dict时:
# -*- coding: utf-8 -*-
import sys
import os
import Tkinter
import numpy as np
from tkFileDialog import askopenfilename
from structDataConfFile import structConfFile
try:
import matlab.engine
except:
print "No matlabEngine installed"
print "Running installation procedure..."
print " "
import installMatlabEngine
print " "
print "Application will exit now..."
print "Please re-run this program to continue installation!"
print " "
exit()
# Environment PATH
Mlab = os.environ.get("MATLAB")
vssFolderPath=os.environ.get("CONTIAME")+'/4_Tools/Veh_Sim_Suite/VSimu'
ContimodelPath=os.environ.get("CONTIAME")
FILETYPES = [ ("text files", "*.mat") ]
handles = dict()
def selectConfFile():
""" Return the path of .mat file
Function that open files browser to choose a .mat file and build handles
dictionary.
"""
Tkinter.Tk().withdraw() # Close the root window
in_path = askopenfilename(filetypes=FILETYPES)
if len(in_path) == 0:
handles['Simulation'] = list()
elif len(in_path) > 0:
handles['Simulation'] = structConfFile(in_path) # HERE I FILL SIMULATION
handles['contimodel_path'] = ContimodelPath
return in_path
def Parametrize(confFile):
""" Return
Argument:
confFile -- str() Configuration File path
Function that call MatLab function passing handles structure/dict as
argument. The return value of MatLab function called VS_Parametrize(handles)
is the modified handles structure.
"""
eng = matlab.engine.start_matlab()
eng.addpath(vssFolderPath)
eng.VS_Parametrise(handles) ############# HERE I CALL VS_PARAMETRIZE
在我的matlab函数handles.simulation
中有结构结构的结构。
在 Python 中,它应该是字典字典的字典,但我收到此错误:
而且我不明白为什么当我在 Python 中加载文件时,数据结构的第一层是 dict,第二层是 numpy.ndarray(第一层:模拟,第二层:参数/信息)。这是有关Matlab 到 Python 变量的更多文档
任何想法 ?
解决方案
推荐阅读
- r - 如何将数据框转换为数值矩阵
- swiftui - 绘制自定义矩形
- php - Laravel 5.8 显示描述和更改图片配置文件
- javascript - 启用/禁用 Firebase 快照侦听器
- amazon-s3 - Amazon-s3:覆盖文件后尝试让 s3 工作
- ruby-on-rails - 如何在docker上将rails与postgresql连接起来?获取:无法连接到服务器:连接被拒绝
- javascript - 在ant Design Form中使用setFieldsValue时无法限制或更改用户输入
- java - 或工具在 Java 中设置具有多个索引的变量
- javascript - JS - 如何创建我的世界风格的云?
- android - 调试应用程序工作时颤动 apk 崩溃