0

I got a .fig file generated in Matlab and I would like to display it in python environment, or at least retrieve the data.

I see that another question (this one) has been asked on this subject, but I am not allowed to comment there because not enough reputation.

In particular the user Sascha proposed the following script:

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()

There are several errors I get when I try to implement it in Spyder 4.1.5 (python 3.7.7), and they do not appear at the same time:

  1. The first error I get was undefined name 'color'. This error was solved by Ander Biguri in a comment here below. However as soon as this error was solved, another appeared.

  2. The second error is cannot import name 'hold' from 'matplotlib.pyplot': apparently the command hold has been deprecated since considered useless. I can avoid the error by commenting the lines of the code where this command appears. Then the following error appears

  3. 'mat_struct' object has no attribute 'String'

I have no idea how to solve this problem. The point is that I would like to have a functioning code. Can anyone help?

Giuseppe P.
  • 21
  • 1
  • 6
  • well, either you are missing an import, of that answer is incorrect. If you just want the data, can'y you just save the data instead of the .fig? – Ander Biguri Jan 19 '21 at 11:36
  • the culprit is likely the line `plot(x,y,marker=mark,linestyle=linestyle,color=color(r,g,b),markersize=marker_size)`. have you tried to just change the way the color is passed? `color=[r,g,b]` for example – Ander Biguri Jan 19 '21 at 11:37
  • @Ander Biguri: the problem is that someone else gave me the .fig. Changing to ```color=[r,g,b]``` solves that error. But now I have another one: ```cannot import name 'hold' from 'matplotlib.pyplot'``` – Giuseppe P. Jan 19 '21 at 11:43
  • well, try to find where it happens and fix the bug.... – Ander Biguri Jan 19 '21 at 11:50
  • Thanks for your help. The best thing would be to be able to comment directly on the original post because the suggested script is full of errors. This policy of "50 reputation at least to comment" is really hard to understand. – Giuseppe P. Jan 19 '21 at 12:15
  • Its not, but that is not a discussion for this comment section. – Ander Biguri Jan 19 '21 at 12:28
  • Does this answer your question? [Sascha's scripts for dysplaing .fig Matlab figure in python](https://stackoverflow.com/questions/65792137/saschas-scripts-for-dysplaing-fig-matlab-figure-in-python) – Ander Biguri Jan 19 '21 at 16:17
  • Unfortunately not, I posted that question after this one. The script giving me troubles is the one in that post. – Giuseppe P. Jan 19 '21 at 16:27
  • That was an automatic message as duplication of questions is not OK in stackoverflow and I voted this one as a duplicate to that one. Please do close it, and next time [edit] this one. – Ander Biguri Jan 19 '21 at 16:43

0 Answers0