Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中访问时如何保存matlab结构?_Python_Matlab_Structure_Mat File_Preserve - Fatal编程技术网

在python中访问时如何保存matlab结构?

在python中访问时如何保存matlab结构?,python,matlab,structure,mat-file,preserve,Python,Matlab,Structure,Mat File,Preserve,我有一个mat文件,我使用 from scipy import io mat = io.loadmat('example.mat') 在matlab中,example.mat包含以下结构 >> load example.mat >> data1 data1 = LAT: [53x1 double] LON: [53x1 double] TIME: [53x1 double

我有一个mat文件,我使用

from scipy import io
mat = io.loadmat('example.mat')
在matlab中,example.mat包含以下结构

    >> load example.mat
    >> data1

    data1 =

            LAT: [53x1 double]
            LON: [53x1 double]
            TIME: [53x1 double]
            units: {3x1 cell}


    >> data2

    data2 = 

            LAT: [100x1 double]
            LON: [100x1 double]
            TIME: [100x1 double]
            units: {3x1 cell}
在matlab中,我可以像data2.LON等一样轻松地访问数据。。它在python中并不是那么简单。它给了我几个选择,虽然像

mat.clear       mat.get         mat.iteritems   mat.keys        mat.setdefault  mat.viewitems   
mat.copy        mat.has_key     mat.iterkeys    mat.pop         mat.update      mat.viewkeys    
mat.fromkeys    mat.items       mat.itervalues  mat.popitem     mat.values      mat.viewvalues    
可以在python中保留相同的结构吗?如果没有,如何最好地访问数据?我目前使用的python代码很难使用


谢谢

找到了关于matlab struct和python的教程

(!)如果嵌套结构保存在
*.mat
文件中,则有必要检查字典中
io.loadmat
输出的项目是否为Matlab结构。例如,如果在Matlab中

>> thisStruct

ans =
      var1: [1x1 struct]
      var2: 3.5

>> thisStruct.var1

ans =
      subvar1: [1x100 double]
      subvar2: [32x233 double]

然后,当我需要从MATLAB将存储在结构数组{struct_1,struct_2}中的数据加载到Python中时,使用mergen in

中的代码。我从使用
scipy.io.loadmat
加载的对象中提取键和值列表。然后,我可以将它们组合到自己的变量中,或者如果需要,将它们重新打包到字典中。并非所有情况下都适合使用
exec
命令,但如果您只是尝试处理数据,它会很好地工作

# Load the data into Python     
D= sio.loadmat('data.mat')

# build a list of keys and values for each entry in the structure
vals = D['results'][0,0] #<-- set the array you want to access. 
keys = D['results'][0,0].dtype.descr

# Assemble the keys and values into variables with the same name as that used in MATLAB
for i in range(len(keys)):
    key = keys[i][0]
    val = np.squeeze(vals[key][0][0])  # squeeze is used to covert matlat (1,n) arrays into numpy (1,) arrays. 
    exec(key + '=val')
#将数据加载到Python中
D=sio.loadmat('data.mat'))
#为结构中的每个条目构建键和值的列表

vals=D['results'][0,0]#这将作为字典返回mat结构

def _check_keys( dict):
"""
checks if entries in dictionary are mat-objects. If yes
todict is called to change them to nested dictionaries
"""
for key in dict:
    if isinstance(dict[key], sio.matlab.mio5_params.mat_struct):
        dict[key] = _todict(dict[key])
return dict


def _todict(matobj):
    """
    A recursive function which constructs from matobjects nested dictionaries
    """
    dict = {}
    for strg in matobj._fieldnames:
        elem = matobj.__dict__[strg]
        if isinstance(elem, sio.matlab.mio5_params.mat_struct):
            dict[strg] = _todict(elem)
        else:
            dict[strg] = elem
    return dict


def loadmat(filename):
    """
    this function should be called instead of direct scipy.io .loadmat
    as it cures the problem of not properly recovering python dictionaries
    from mat files. It calls the function check keys to cure all entries
    which are still mat-objects
    """
    data = sio.loadmat(filename, struct_as_record=False, squeeze_me=True)
    return _check_keys(data)

您能解释一下当您将它加载到python中时它是什么样子吗?还有,另一个想法。如果您正在使用SciPi,您是否尝试过使用
SciPi.loadmat
?是的,我尝试过loadmat。python中的输出很难使用。我甚至不知道如何在data1或data2中访问LON或LAT。@mikeP——我猜可能只是
data1['LAT']
(python)而不是
data1.LAT
(matlab)。如果您仍然有问题(并且不介意/可以共享)请随意传递一个示例.mat文件,我下班回家后会处理它。这可能还可以为您添加一层附加信息: