I have a .mat file generated from matlab 2012b. It contains a variable with a user-defined matlab class.
我有一个从matlab 2012b生成的。mat文件。它包含一个带有用户定义的matlab类的变量。
When loading the file using scipy.io.loadmat in python 3.3, I get the following:
使用scipy.io加载文件时。在python 3.3中的loadmat,我得到如下内容:
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:
我希望访问“cStream”类型的“futureDS”对象,但似乎无法使用mat['None']来访问。调用mat['没有']简单的结果:
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.
我困在这里。我是python的新手,尝试从matlab移植我的旧作品。如有任何帮助,我们将不胜感激。
Thank you.
谢谢你!
1 个解决方案
#1
4
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.
不幸的是,SciPy不支持包含新样式类对象(用classdef定义的)的mat文件,就我所知,也不支持任何第三方的mat文件阅读器。返回的mat字典中的__function_workspace__元素包含您需要的一些未文档化的、尚未反向工程的信息。
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.
最简单的解决方案是在将您的自定义类转换为基本的struct对象后,再将它们保存到磁盘。这可以通过简单地调用struct(futureDS)来实现(尽管有警告)。它将所有公共和私有属性公开为普通字段,任何第三方读者都可以阅读它。
(More technically, Matlab saves these objects with the Matlab Array type id of 17; the official documentation (PDF) only enumerates types up through 15).
(更严格地说,Matlab用Matlab数组类型id为17保存这些对象;官方文档(PDF)只列举了15种类型。
#1
4
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.
不幸的是,SciPy不支持包含新样式类对象(用classdef定义的)的mat文件,就我所知,也不支持任何第三方的mat文件阅读器。返回的mat字典中的__function_workspace__元素包含您需要的一些未文档化的、尚未反向工程的信息。
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.
最简单的解决方案是在将您的自定义类转换为基本的struct对象后,再将它们保存到磁盘。这可以通过简单地调用struct(futureDS)来实现(尽管有警告)。它将所有公共和私有属性公开为普通字段,任何第三方读者都可以阅读它。
(More technically, Matlab saves these objects with the Matlab Array type id of 17; the official documentation (PDF) only enumerates types up through 15).
(更严格地说,Matlab用Matlab数组类型id为17保存这些对象;官方文档(PDF)只列举了15种类型。