0

I am building a software and a part of this software needs to read a .mat file and give the content to a matlab function as argument. The problem is that I have bad data structure I guess.

In fact my Matlab structure called handles doesn't have the same data structure than my handles in my Python (after conversion)

When I load the .mat file from python with scipy.io.loadmat(), I have those 4 keys:

enter image description here

So in handles.simulation there supposed to be this content:

enter image description here

enter image description here

So I use matlab.engine to call matlab function in my case the function is called VS_Parametrise and when I pass my handles which is a 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

In my matlab function handles.simulation there are structure of structure of structure.

In Python it should be dictionary of dictionary of dictionary but I get this error:

enter image description here

And I don't understand why when i load the file in Python the first floor of the data structure is a dict and the second is a numpy.ndarray (1st floor: simulation, 2nd floor: parameter/info). Here is more documentation about Matlab to Python variable

Any idea ?

2
  • 1
    scipy.io.loadmat loads MATLAB matrices as numpy arrays (ndarray), cells and structs as some combination of numpy object dtype arrays. MATLAB functions, classes and special types don't have Python/numpy equivalents and don't load (well). In other words, it's a means of transferring largely numeric data. Commented Jul 5, 2019 at 15:44
  • @hpaulj Ok so i guess in my case, it will be difficult to me to manage my handles variable between Python and Matlab :/ Commented Jul 8, 2019 at 7:47

0

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.