[SciPy-User] loadmat and figure

Fabrice Silva silva at lma.cnrs-mrs.fr
Sun Aug 5 06:29:31 EDT 2012


Le dimanche 05 août 2012 à 11:50 +0200, x.piter at gmail.com a écrit :
> Hi
> Fabrice Silva <silva at lma.cnrs-mrs.fr> writes:
> > Are not matlab figure supposed to be .fig files ?
> > The 'a' items of the resulting dictionary seems weird.
> I was not sure if fig files are compartiple with loadmat, so I had
> loaded figure in matlab with -mat key and saved the structure a.mat

Debian unstable ships scipy 0.10 which succeed in loading .fig files.
Example :

        In [1]: import scipy; scipy.__version__
        Out[1]: '0.10.1'
        
        In [2]: import scipy.io as io
        
        In [3]: d = io.loadmat('AR.fig', squeeze_me=True, struct_as_record=False); d.keys()
        Out[3]: ['hgS_070000', '__version__', '__header__', '__globals__']
                
Note the hgS_* magic key indicating matlab v7 figure type
        
        In [4]: print d['__header__']
        MATLAB 5.0 MAT-file, Platform: GLNXA64, Created on: Fri Jul  6 13:43:19 2012 
        
        In [5]: print d['hgS_070000'].type
        figure
                
You then need to introspect through the structured hierarchy to extract your data
        
        In [6]: print d['hgS_070000'].children.type
        axes
        
        In [7]: print d['hgS_070000'].children.children.shape
        (43,)
        
        In [8]: print d['hgS_070000'].children.children[0].type
        graph2d.lineseries
        
        In [9]: dir(d['hgS_070000'].children.children[0].properties)
        Out[9]: 
        ['ApplicationData',
         'CodeGenColorMode',
         'CodeGenLineStyleMode',
         'CodeGenMarkerMode',
         'Color',
         'LineWidth',
         'ObeyXDataMode',
         'OldSwitchProps',
         'OldSwitchVals',
         'SwitchProps',
         'XData',
         'XDataJitter',
         'XDataMode',
         'XDataSource',
         'YData',
         'YDataSource',
         'ZDataSource',
         '__class__',
         '__delattr__',
         '__dict__',
         '__doc__',
         '__format__',
         '__getattribute__',
         '__hash__',
         '__init__',
         '__module__',
         '__new__',
         '__reduce__',
         '__reduce_ex__',
         '__repr__',
         '__setattr__',
         '__sizeof__',
         '__slotnames__',
         '__str__',
         '__subclasshook__',
         '__weakref__',
         '_fieldnames']
                
Getting X and Y arrays for the first line of the first axes
        
        In [10]: X = d['hgS_070000'].children.children[0].properties.XData
        
        In [11]: Y = d['hgS_070000'].children.children[0].properties.YData
        
        In [12]: import matplotlib.pyplot as plt
        
        In [13]: plt.plot(X,Y)
        Out[13]: [<matplotlib.lines.Line2D at 0xa164b8c>]

> This is another trouble but I will think about it when I reach this
> stage (I need only numbers). May be I need the latest scipy.

Have you seen the note at the end of this page, concerning v7.3 format
not handled by scipy 0.7 (also neither by recent versions) ?
http://docs.scipy.org/doc/scipy-0.7.x/reference/generated/scipy.io.loadmat.html#scipy.io.loadmat


-- 
Fabrice Silva <silva at lma.cnrs-mrs.fr>
LMA UPR CNRS 7051




More information about the SciPy-User mailing list