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 files are .mat files (containing a struct), see
http://undocumentedmatlab.com/blog/fig-files-format/
As the reference you give states, structs are only supported up to v7.1:
http://www.scipy.org/Cookbook/Reading_mat_files
So, in MATLAB I save using -v7:
plot([1 2],[3 4])
hgsave(gcf,'c','-v7');
Then in Python 2.6.4 I use:
>>> from scipy.io import loadmat
>>> x = loadmat('c.fig')
>>> x
{'hgS_070000': array([[]], 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([[]], dtype=object), '_fieldnames': ['type', 'handle', 'properties', 'children', 'special'], 'type': array([u'figure'], dtype=']], dtype=object), 'special': array([], shape=(1, 0), dtype=float64)}
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)
Showing that I'd used plot([1 2],[3 4]) in MATLAB (the child is the axis and the grandchild is the lineseries).