python如何打开mat文件,在python中访问包含matlab类的.mat文件

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).

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值