使用Python从MATLAB .fig文件中获取数据? [英] Data from a MATLAB .fig file using Python?

查看:248
本文介绍了使用Python从MATLAB .fig文件中获取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道使用Python从MATLAB图文件中提取数据的任何方法吗?我知道这些是二进制文件,但是Python手册中.mat文件的方法 http://www.scipy. org/Cookbook/Reading_mat_files 似乎不适用于.fig文件...

Does anyone know of any methods of extracting the data from a MATLAB fig file using Python? I know these are binary files but the methods in the Python Cookbook for .mat files http://www.scipy.org/Cookbook/Reading_mat_files don't seem to work for .fig files...

预先感谢您的帮助, 丹

Thanks in advance for any help, Dan

推荐答案

.fig文件是.mat文件(包含结构),请参见 http://undocumentedmatlab.com/blog/fig-files-format/

.fig files are .mat files (containing a struct), see http://undocumentedmatlab.com/blog/fig-files-format/

作为您提供参考的状态,仅在v7.1之前支持结构: http://www.scipy.org/Cookbook/Reading_mat_files

As the reference you give states, structs are only supported up to v7.1: http://www.scipy.org/Cookbook/Reading_mat_files

因此,在MATLAB中,我使用-v7保存:

So, in MATLAB I save using -v7:

plot([1 2],[3 4])
hgsave(gcf,'c','-v7');

然后在Python 2.6.4中使用:

Then in Python 2.6.4 I use:

>>> from scipy.io import loadmat
>>> x = loadmat('c.fig')
>>> x
{'hgS_070000': array([[<scipy.io.matlab.mio5.mat_struct object at 0x1500e70>]], dtype=object), '__version__': '1.0', '__header__': 'MATLAB 5.0 MAT-file, Platform: MACI64, Created on: Fri Nov 18 12:02:31 2011', '__globals__': []}
>>> x['hgS_070000'][0,0].__dict__
{'handle': array([[1]], dtype=uint8), 'children': array([[<scipy.io.matlab.mio5.mat_struct object at 0x1516030>]], dtype=object), '_fieldnames': ['type', 'handle', 'properties', 'children', 'special'], 'type': array([u'figure'], dtype='<U6'), 'properties': array([[<scipy.io.matlab.mio5.mat_struct object at 0x1500fb0>]], dtype=object), 'special': array([], shape=(1, 0), dtype=float64)}

我在哪里使用.__dict__来查看如何遍历结构.例如.要获得XDataYData,我可以使用:

Where I used .__dict__ to see how to traverse the structure. E.g. to get XData and YData I can use:

>>> x['hgS_070000'][0,0].children[0,0].children[0,0].properties[0,0].XData
array([[1, 2]], dtype=uint8)
>>> x['hgS_070000'][0,0].children[0,0].children[0,0].properties[0,0].YData
array([[3, 4]], dtype=uint8)

表明我在MATLAB中使用了plot([1 2],[3 4])(子代是轴,孙子是线系列).

Showing that I'd used plot([1 2],[3 4]) in MATLAB (the child is the axis and the grandchild is the lineseries).

这篇关于使用Python从MATLAB .fig文件中获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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