如何用Python读取MATLAB .fig文件中的数据?

15 投票
6 回答
24607 浏览
提问于 2025-04-17 06:31

有没有人知道怎么用Python从MATLAB的fig文件中提取数据?我知道这些文件是二进制格式的,但Python Cookbook里关于.mat文件的方法http://www.scipy.org/Cookbook/Reading_mat_files似乎不适用于.fig文件……

提前谢谢大家的帮助,
Dan

6 个回答

8

我觉得Alex的回答很不错,但我对他的代码做了一些扩展。首先,我加了一些前言,说明图形、y轴标签等是从哪里来的。其次,我还加上了图例!我对Python还比较陌生,所以任何改进的建议都非常欢迎。

def plotFig(filename,fignr=1):
    from scipy.io import loadmat
    from numpy import size
    from matplotlib.pyplot import plot,figure,hold,xlabel,ylabel,show,clf,xlim,legend
    d = loadmat(filename,squeeze_me=True, struct_as_record=False)
    ax1 = d['hgS_070000'].children
    if size(ax1) > 1:
        legs= ax1[1]
        ax1 = ax1[0]
    else:
        legs=0
    figure(fignr)
    clf()
    hold(True)
    counter = 0    
    for line in ax1.children:
        if line.type == 'graph2d.lineseries':
            if hasattr(line.properties,'Marker'):
                mark = "%s" % line.properties.Marker
                mark = mark[0]
            else:
                mark = '.'
            if hasattr(line.properties,'LineStyle'):
                linestyle = "%s" % line.properties.LineStyle
            else:
                linestyle = '-'
            if hasattr(line.properties,'Color'):
                r,g,b =  line.properties.Color
            else:
                r = 0
                g = 0
                b = 1
            if hasattr(line.properties,'MarkerSize'):
                marker_size = line.properties.MarkerSize
            else:
                marker_size = 1                
            x = line.properties.XData
            y = line.properties.YData
            plot(x,y,marker=mark,linestyle=linestyle,color=color(r,g,b),markersize=marker_size)
        elif line.type == 'text':
            if counter < 1:
                xlabel("%s" % line.properties.String,fontsize =16)
                counter += 1
            elif counter < 2:
                ylabel("%s" % line.properties.String,fontsize = 16)
                counter += 1        
    xlim(ax1.properties.XLim)
    if legs:        
        leg_entries = tuple(legs.properties.String)
        py_locs = ['upper center','lower center','right','left','upper right','upper left','lower right','lower left','best']
        MAT_locs=['North','South','East','West','NorthEast', 'NorthWest', 'SouthEast', 'SouthWest','Best']
        Mat2py = dict(zip(MAT_locs,py_locs))
        location = legs.properties.Location
        legend(leg_entries,loc=Mat2py[location])
    hold(False)
    show()
11

这是我对Sascha帖子的一些更新。现在它可以:

  • 显示旋转的文本标签
  • 显示x轴和y轴的刻度
  • 更好地处理标记
  • 可以开关网格线
  • 更好地处理坐标轴和图例的编号
  • 保持图形的大小

下面是代码:

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

def plotFig(filename,fignr=1):
   d = loadmat(filename,squeeze_me=True, struct_as_record=False)
   matfig = d['hgS_070000']
   childs = matfig.children
   ax1 = [c for c in childs if c.type == 'axes']
   if(len(ax1) > 0):
       ax1 = ax1[0]
   legs = [c for c in childs if c.type == 'scribe.legend']
   if(len(legs) > 0):
       legs = legs[0]
   else:
       legs=0
   pos = matfig.properties.Position
   size = np.array([pos[2]-pos[0],pos[3]-pos[1]])/96
   plt.figure(fignr,figsize=size)
   plt.clf()
   plt.hold(True)
   counter = 0    
   for line in ax1.children:
       if line.type == 'graph2d.lineseries':
           if hasattr(line.properties,'Marker'):
               mark = "%s" % line.properties.Marker
               if(mark != "none"):
                   mark = mark[0]
           else:
               mark = '.'
           if hasattr(line.properties,'LineStyle'):
               linestyle = "%s" % line.properties.LineStyle
           else:
               linestyle = '-'
           if hasattr(line.properties,'Color'):
               r,g,b =  line.properties.Color
           else:
               r = 0
               g = 0
               b = 1
           if hasattr(line.properties,'MarkerSize'):
               marker_size = line.properties.MarkerSize
           else:
               marker_size = -1                
           x = line.properties.XData
           y = line.properties.YData
           if(mark == "none"):
               plt.plot(x,y,linestyle=linestyle,color=[r,g,b])
           elif(marker_size==-1):
               plt.plot(x,y,marker=mark,linestyle=linestyle,color=[r,g,b])
           else:
               plt.plot(x,y,marker=mark,linestyle=linestyle,color=[r,g,b],ms=marker_size)
       elif line.type == 'text':
           if counter == 0:
               plt.xlabel("$%s$" % line.properties.String,fontsize =16)
           elif counter == 1:
               plt.ylabel("$%s$" % line.properties.String,fontsize = 16)
           elif counter == 3:
               plt.title("$%s$" % line.properties.String,fontsize = 16)
           counter += 1        
   plt.grid(ax1.properties.XGrid)
   
   if(hasattr(ax1.properties,'XTick')):
       if(hasattr(ax1.properties,'XTickLabelRotation')):
           plt.xticks(ax1.properties.XTick,ax1.properties.XTickLabel,rotation=ax1.properties.XTickLabelRotation)
       else:
           plt.xticks(ax1.properties.XTick,ax1.properties.XTickLabel)
   if(hasattr(ax1.properties,'YTick')):
       if(hasattr(ax1.properties,'YTickLabelRotation')):
           plt.yticks(ax1.properties.YTick,ax1.properties.YTickLabel,rotation=ax1.properties.YTickLabelRotation)
       else:
           plt.yticks(ax1.properties.YTick,ax1.properties.YTickLabel)
   plt.xlim(ax1.properties.XLim)
   plt.ylim(ax1.properties.YLim)
   if legs:        
       leg_entries = tuple(['$' + l + '$' for l in legs.properties.String])
       py_locs = ['upper center','lower center','right','left','upper right','upper left','lower right','lower left','best','best']
       MAT_locs=['North','South','East','West','NorthEast', 'NorthWest', 'SouthEast', 'SouthWest','Best','none']
       Mat2py = dict(zip(MAT_locs,py_locs))
       location = legs.properties.Location
       plt.legend(leg_entries,loc=Mat2py[location])
   plt.hold(False)
   plt.show()

更新适用于python 3.x及以上版本,(请查看@robert-pollak的评论):

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

def plotFig(filename,fignr=1, subfig=1):
   d = loadmat(filename,squeeze_me=True, struct_as_record=False)
   matfig = d['hgS_070000']
   childs = matfig.children

   sfig = max(0, subfig - 1)
   
   ax1 = [c for c in childs if c.type == 'axes']
   if(len(ax1) > 0):
       sfig = min(sfig, len(ax1) - 1)
       ax1 = ax1[sfig]
   
   legs = [c for c in childs if c.type == 'scribe.legend']
   if(len(legs) > 0):
       legs = legs[sfig]
   else:
       legs=0
   pos = matfig.properties.Position
   size = np.array([pos[2]-pos[0],pos[3]-pos[1]])/96
   plt.figure(fignr,figsize=size)
   plt.clf()
   #plt.hold(True)
   counter = 0    
   for line in ax1.children:
       if line.type == 'graph2d.lineseries':
           if hasattr(line.properties,'Marker'):
               mark = "%s" % line.properties.Marker
               if(mark != "none"):
                   mark = mark[0]
           else:
               mark = '.'
           if hasattr(line.properties,'LineStyle'):
               linestyle = "%s" % line.properties.LineStyle
           else:
               linestyle = '-'
           if hasattr(line.properties,'Color'):
               r,g,b =  line.properties.Color
           else:
               r = 0
               g = 0
               b = 1
           if hasattr(line.properties,'MarkerSize'):
               marker_size = line.properties.MarkerSize
           else:
               marker_size = -1                
           x = line.properties.XData
           y = line.properties.YData
           if(mark == "none"):
               plt.plot(x,y,linestyle=linestyle,color=[r,g,b])
           elif(marker_size==-1):
               plt.plot(x,y,marker=mark,linestyle=linestyle,color=[r,g,b])
           else:
               plt.plot(x,y,marker=mark,linestyle=linestyle,color=[r,g,b],ms=marker_size)
       elif line.type == 'text':
           if counter == 0:
               plt.xlabel("$%s$" % line.properties.String,fontsize =16)
           elif counter == 1:
               plt.ylabel("$%s$" % line.properties.String,fontsize = 16)
           elif counter == 3:
               plt.title("$%s$" % line.properties.String,fontsize = 16)
           counter += 1        
   plt.grid(ax1.properties.XGrid)

   if(hasattr(ax1.properties,'XTick')):
       if(hasattr(ax1.properties,'XTickLabelRotation')):
           plt.xticks(ax1.properties.XTick,ax1.properties.XTickLabel,rotation=ax1.properties.XTickLabelRotation)
       else:
           plt.xticks(ax1.properties.XTick,ax1.properties.XTickLabel)
   if(hasattr(ax1.properties,'YTick')):
       if(hasattr(ax1.properties,'YTickLabelRotation')):
           plt.yticks(ax1.properties.YTick,ax1.properties.YTickLabel,rotation=ax1.properties.YTickLabelRotation)
       else:
           plt.yticks(ax1.properties.YTick,ax1.properties.YTickLabel)
   plt.xlim(ax1.properties.XLim)
   plt.ylim(ax1.properties.YLim)
   if legs:        
       leg_entries = tuple(['$' + l + '$' for l in legs.properties.String])
       py_locs = ['upper center','lower center','right','left','upper right','upper left','lower right','lower left','best','best']
       MAT_locs=['north','south','east','west','northeast', 'northwest', 'southeast', 'southwest','best','none']
       Mat2py = dict(zip(MAT_locs,py_locs))
       location = legs.properties.Location.lower()
       plt.legend(leg_entries,loc=Mat2py[ location ])
   #plt.hold(False)
   plt.show()
12

.fig 文件其实就是 .mat 文件(里面包含一个结构体),你可以查看这个链接了解更多:http://undocumentedmatlab.com/blog/fig-files-format/

正如你提到的参考资料所说,结构体在 v7.1 版本之前是支持的,具体可以看这个链接:http://www.scipy.org/Cookbook/Reading_mat_files

所以,在 MATLAB 中我使用 -v7 来保存文件:

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

然后在 Python 2.6.4 中我使用:

>>> 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,我可以这样做:

>>> 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])(这里的子对象是坐标轴,孙对象是线条系列)。

撰写回答