[SciPy-User] Matlab figure

Aronne Merrelli aronne.merrelli@gmail....
Thu Aug 30 11:33:42 CDT 2012


On Wed, Aug 29, 2012 at 1:16 PM, Nils Wagner <nils106@googlemail.com> wrote:
> Hi all,
>
> I have received a Matlab figure stored in a matlab file.
> How can I extract the curves from the file ?
>

I guess it depends on what exactly you mean by "figure stored in a
matlab file". I assume you mean save as fig-format; after I fiddling
around for a bit, I find that this seems to work:

>From MATLAB:
>> x = sqrt(1:9);
>> plot(x)
>> saveas(gcf(), 'test.fig', 'fig')
>> version
ans =
7.9.0.529 (R2009b)


SciPy:

In [11]: from scipy.io.matlab import loadmat
In [12]: m = loadmat('test.fig')
In [13]: m.keys()
Out[13]: ['hgS_070000', '__version__', '__header__', '__globals__']

In [15]: m['hgS_070000']['children'].item()['children'].item()['properties'].item()['XData'].item()
Out[15]: array([[1, 2, 3, 4, 5, 6, 7, 8, 9]], dtype=uint8)

In [16]: m['hgS_070000']['children'].item()['children'].item()['properties'].item()['YData'].item()
Out[16]:
array([[ 1.        ,  1.41421356,  1.73205081,  2.        ,  2.23606798,
         2.44948974,  2.64575131,  2.82842712,  3.        ]])

In [17]: np.version.version
Out[17]: '1.6.1'
In [18]: scipy.version.version
Out[18]: '0.9.0'


I have no idea where the key name 'hgS_070000' comes from, but it
should be obvious to find if the figure is the only thing saved in the
file, since it would be the only variable. The rest of the syntax
crawling through the data structure matches how the figure is built in
MATLAB. Meaning, if you load it back, you would have done the same
thing in MATLAB like so: (Note I renamed it to test.mat to get MATLAB
to reload it here)

>> m = load('test.mat')
>> m.hgS_070000.children.children.properties.XData
ans =
     1     2     3     4     5     6     7     8     9

>> m.hgS_070000.children.children.properties.YData
ans =
  Columns 1 through 8
    1.0000    1.4142    1.7321    2.0000    2.2361    2.4495    2.6458    2.8284
  Column 9
    3.0000


If you reloaded it via the GUI load figure, and it is in figure #2,
then it would have been:
>> get(get(get(2,'Children'),'Children'),'XData')
etc...



Cheers,
Aronne


More information about the SciPy-User mailing list