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:
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.The second error is
cannot import name 'hold' from 'matplotlib.pyplot'
: apparently the commandhold
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'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?