在python中访问包含matlab类的.mat文件 [英] Access .mat file containing matlab classes in python

查看:2180
本文介绍了在python中访问包含matlab类的.mat文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.mat文件从matlab生成2012b。它包含一个带有用户定义的matlab类的变量。



在python 3.3中使用scipy.io.loadmat加载文件时,我得到以下结果:

  mat = scipy.io.loadmat('D:\test.mat')
mat
{'__header__ ':b'MATLAB 5.0 MAT文件,平台:PCWIN64,创建日期:Fri Feb 22 15:26:28 2013','__function_workspace__':array([[0,1,73,...,0, 0]],dtype = uint8),'__globals__':[],'__version__':'1.0','None':MatlabOpaque([b'futureDS',b'MCOS',b'cStream',[[3707764736 ]',
dtype = [('s0','O'),('s1','O',[2],[1],[1] ),('s2','O'),('arr','O')])

我想要访问类型cStream的futureDS对象,但似乎无法这样使用mat ['无']。调用mat ['None']简单的结果是:

  MatlabOpaque([(b'futureDS',b'MCOS',b 'cStream',[[3707764736],[2],[1],[1],[1],[1]]],
dtype = [('s0','O'), 's1','O'),('s2','O'),('arr','O')])

我被困在这里。我是新的python和尝试从我的旧工作从matlab。

解决方案

不幸的是, ,SciPy不支持包含新式类对象(用 classdef 定义的对象)的mat文件,也不支持任何第三方mat文件读取器知道的。在返回的 mat 字典中的 __ function_workspace __ 元素包含您在一些未记录和尚未



最简单的解决方案是在Matlab中将您的自定义类转换为基本的 struct 对象将其保存到磁盘。这可以通过简单地调用 struct(futureDS)来实现(尽管有警告)。它将所有公共和私有属性暴露为纯字段,任何第三方读者都可以阅读它的价值。



(更技术上,Matlab使用Matlab数组类型ID为17; 官方文档(PDF)仅列举类型到15)。


I have a .mat file generated from matlab 2012b. It contains a variable with a user-defined matlab class.

When loading the file using scipy.io.loadmat in python 3.3, I get the following:

mat=scipy.io.loadmat('D:\test.mat')
mat
{'__header__': b'MATLAB 5.0 MAT-file, Platform: PCWIN64, Created on: Fri Feb 22 15:26:28 2013', '__function_workspace__': array([[ 0,  1, 73, ...,  0,  0,  0]], dtype=uint8), '__globals__': [], '__version__': '1.0', 'None': MatlabOpaque([ (b'futureDS', b'MCOS', b'cStream', [[3707764736], [2], [1], [1], [1], [1]])], 
      dtype=[('s0', 'O'), ('s1', 'O'), ('s2', 'O'), ('arr', 'O')])}

I am looking to access the "futureDS" object of type "cStream" but seem unable to do so using mat['None']. Calling mat['None'] simple results in:

MatlabOpaque([ (b'futureDS', b'MCOS', b'cStream', [[3707764736], [2], [1], [1], [1], [1]])], 
      dtype=[('s0', 'O'), ('s1', 'O'), ('s2', 'O'), ('arr', 'O')])

I am stuck here. I am new to python and trying to port my old work from matlab. Any help would be appreciated.

Thank you.

解决方案

Unfortunately, SciPy does not support mat files that contain new-style class objects (those defined with classdef), nor does any third-party mat-file reader as far as I'm aware. That __function_workspace__ element in the returned mat dictionary contains the information you're after in some undocumented and as-yet-not-reverse-engineered way.

The easiest solution is to convert your custom classes into basic struct objects within Matlab before saving them to disk. This can be achieved (albeit with a warning) by simply calling struct(futureDS). It exposes all public and private properties as plain fields, which can be read by any third-party reader worth its salt.

(More technically, Matlab saves these objects with the Matlab Array type id of 17; the official documentation (PDF) only enumerates types up through 15).

这篇关于在python中访问包含matlab类的.mat文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆