(no subject)

Hi,

While moving from Matlab to Numpy/Scipy/Matplotlib I need sometimes to work with Matlab figures. It would be nice if we could load Matlab figures in Python, extract the data and some basic parameters of how it was looking in Matlab and create its "clone" in matplotlib. At present the Matlab figures are MAT files and in the near future it will be HDF5, both formats are loadable. However, I have not found any reference for such attempts to load and parse the FIG files. As a beginner I find it difficult to write a large piece of code. However, if there are other interested users that can cooperate on such, I'd gladly contribute some hours for this task. Meanwhile, to show the proof-of-concept attempt is attached below. All your useful comments and suggestions are very welcome.

Thank you,
Alex

#------------ loadfig.py ---------------------- #
""" Loadfig loads simple Matlab figures as MAT files and plots the lines using matplotlib

"""
import numpy as np
from scipy.io import loadmat
import matplotlib.pyplot as plt

def lowest_order(d):
  """
  lowestsc_order(hgstruct) finds the handle of axes (i.e. children of figure handles, etc.)
  
  """
  while not 'graph2d.lineseries' in d['type']:
    d = d['children'][0][0]
  return d
  
def get_data(d):
  """
  get_data(hgstruct) extracts XData, YData, Color and Marker of each line in the
  given axis
  
  Parameters

···

----------
  hgstruct : structure
    obtained from lowest_order(loadmat('matlab.fig'))
  
  """
  xd,yd,dispname,marker,colors = [],[],[],[],[]
  for i in d:
    if i['type'] == 'graph2d.lineseries':
      xd.append(i['properties'][0][0]['XData'][0][0])
      yd.append(i['properties'][0][0]['YData'][0][0])
      dispname.append(i['properties'][0][0]['DisplayName'][0][0])
      marker.append(i['properties'][0][0]['Marker'][0][0])
      colors.append(i['properties'][0][0]['Color'][0][0])
      
  return np.asarray(xd),np.asarray(yd),dispname,np.asarray(marker).astype('S1'),colors

def plot_data(xd,yd,dispname=None,marker=None,colors=None):
  """
  plot_data(xd,yd,dispname=None,marker=None,colors=None)
  
  plots the data sets extracted by get_data(lowest_order(loadmat('matlab.fig')))
  
  Parameters
  ----------
    xd,yd : array_like
      data arrays
    dispname : array of strings
      to be used in legend, optional
    marker : array of characters
      markers, e.g. ['o','x'], optional
    colors : array of color sets
      in RGB, e.g. [[0,0,1],[1,0,0]], optional
      
  """
  for i,n in enumerate(xd):
    plt.plot(xd[i].T,yd[i].T,color=tuple(colors[i]),marker=marker[i],linewidth=0)
    
  plt.legend(dispname)
  plt.show()

def main(filename):
  """
  main(filename)
  
  loads the filename (with the extension .fig) which is Matlab figure. At the moment only
  simple 2D lines are supported.
  
  Examples
  -------
  >>> loadfig('matlab.fig') # is equivalent to:
  
  >>> d = loadmat(filename)
  >>> d = d['hgS_070000']
  >>> xd,yd,dispname,marker,colors = get_data(lowest_order(d))
  >>> plot_data(xd,yd,dispname,marker,colors)
  
  """
  d = loadmat(filename)
  # ver 7.2 or lower:
  d = d['hgS_070000']
  xd,yd,dispname,marker,colors = get_data(lowest_order(d))
  plot_data(xd,yd,dispname,marker,colors)

if __name__ == "__main__":
    import sys
    import os
    try:
      filename = sys.argv[1]
         main(filename)
    except:
      print("Wrong file")
# ----------------------------- EOF loadfig.py --------------------------- #

Alex Liberzon
Turbulence Structure Laboratory [http://www.eng.tau.ac.il/efdl]
School of Mechanical Engineering
Tel Aviv University
Ramat Aviv 69978
Israel
Tel: +972-3-640-8928
Lab: +972-3-640-6860 (telefax)
E-mail: alexlib@...3412...

Alex,

That is very interesting. I was not aware that matlab’s figure files were simply .mat files in disguise. I would presume that it would be feasible to produce some sort of importer in such a case (provided the documentation for Matlab’s figure format is complete enough).

I am wary of making such a function a core feature of Matplotlib, however, because it would require creating a dependency to the scipy.io package. However, I could see it being a toolkit package like Basemap.

Would you mind creating a feature request ticket at:

http://sourceforge.net/tracker/?group_id=80706

If you include this example code, and maybe some links to some documentation on matlab’s figure files, maybe something can grow from that.

Thanks!
Ben Root

···

On Tue, Jan 18, 2011 at 5:32 PM, Alex Liberzon <alex.liberzon@…287…> wrote:

Hi,

While moving from Matlab to Numpy/Scipy/Matplotlib I need sometimes to work with Matlab figures. It would be nice if we could load Matlab figures in Python, extract the data and some basic parameters of how it was looking in Matlab and create its “clone” in matplotlib. At present the Matlab figures are MAT files and in the near future it will be HDF5, both formats are loadable. However, I have not found any reference for such attempts to load and parse the FIG files. As a beginner I find it difficult to write a large piece of code. However, if there are other interested users that can cooperate on such, I’d gladly contribute some hours for this task. Meanwhile, to show the proof-of-concept attempt is attached below. All your useful comments and suggestions are very welcome.

Thank you,

Alex

I agree, but for a slightly different reason. I think it would be
great if it would be possible to incorporate viewing matlab figures
into the rest of my system, so making it more isolated would help in
this regard.

-Todd

···

On Wed, Jan 19, 2011 at 10:16 AM, Benjamin Root <ben.root@...1304...> wrote:

On Tue, Jan 18, 2011 at 5:32 PM, Alex Liberzon <alex.liberzon@...287...> > wrote:

Hi,

While moving from Matlab to Numpy/Scipy/Matplotlib I need sometimes to
work with Matlab figures. It would be nice if we could load Matlab figures
in Python, extract the data and some basic parameters of how it was looking
in Matlab and create its "clone" in matplotlib. At present the Matlab
figures are MAT files and in the near future it will be HDF5, both formats
are loadable. However, I have not found any reference for such attempts to
load and parse the FIG files. As a beginner I find it difficult to write a
large piece of code. However, if there are other interested users that can
cooperate on such, I'd gladly contribute some hours for this task.
Meanwhile, to show the proof-of-concept attempt is attached below. All your
useful comments and suggestions are very welcome.

Thank you,
Alex

Alex,

That is very interesting. I was not aware that matlab's figure files were
simply .mat files in disguise. I would presume that it would be feasible to
produce some sort of importer in such a case (provided the documentation for
Matlab's figure format is complete enough).

I am wary of making such a function a core feature of Matplotlib, however,
because it would require creating a dependency to the scipy.io package.
However, I could see it being a toolkit package like Basemap.

Would you mind creating a feature request ticket at:

matplotlib download | SourceForge.net

If you include this example code, and maybe some links to some documentation
on matlab's figure files, maybe something can grow from that.

Thanks!
Ben Root