from scipy.io import loadmat
import pandas as pd
import matplotlib.pyplot as plt
def conv_matlab_to_csv(matlab_file):
"""
This function converts a matlab file into a csv file."
You need to pass the matlab file path as the parameter.
"""
data = loadmat(matlab_file)
cols=[col for col in data if '__' not in col]
df=pd.DataFrame(columns=cols)
for i in data:
if '__' not in i :
df[i]=(data[i]).ravel()
#save to csv without the pandas index
return df.to_csv("mat_converted_to_csv.csv", index=False)
def conv_matlab_to_df(matlab_file):
"""
This function converts a matlab file into a csv file."
You need to pass the matlab file path as the parameter.
"""
data = loadmat(matlab_file)
cols=[col for col in data if '__' not in col]
df=pd.DataFrame(columns=cols)
for i in data:
if '__' not in i :
df[i]=(data[i]).ravel()
return df
def plot_line(df1, df2):
df_line1["Mymean1"].plot.line()
df_line2["Mymean2"].plot.line()
font1 = {'family':'serif','color':'black','size':20}
font2 = {'family':'serif','color':'darkred','size':15}
plt.title("Mymean", fontdict = font1)
plt.grid()
plt.xlabel("Values", fontdict = font2)
plt.ylabel("Angles", fontdict = font2)
plt.show()
matlab_file = 'myMat1.mat'
df_line1 = conv_matlab_to_df(matlab_file)
matlab_file = 'myMat2.mat'
df_line2 = conv_matlab_to_df(matlab_file)
plot_line(df_line1, df_line2)
Hi Adam,
This is quite amazing that you were the one returning my message. I was actually following your videos but not sure how to move forward since I am new to this tool. I am attaching the main jupyter notebook file to this email for your reference.
I have matlab files that I need to plot the data frames from each of the matlab files
The matlab .mat files were converted to csv and plotted
I also attached two mat files for you.
The python codes are working
Again, it is a great honor that you replied to my email.
Thank you
Tito
hi Tito,
You can create an empty assets
folder and insert the myMat2.mat
file in there. Then, create a python file called app.py outside the assets folder with this code below. If you run the python file, your app should be able to launch, which would convert the mat file to csv and read it into a plotly graph.
from scipy.io import loadmat
import pandas as pd
import plotly.express as px
from dash import Dash, html, dcc
app = Dash(__name__)
def conv_matlab_to_csv(matlab_file):
"""
This function converts a matlab file into a csv file."
You need to pass the matlab file path as the parameter.
"""
data = loadmat(matlab_file)
cols=[col for col in data if '__' not in col]
df=pd.DataFrame(columns=cols)
for i in data:
if '__' not in i :
df[i]=(data[i]).ravel()
#save to csv without the pandas index
return df.to_csv("mat3_converted_to_csv.csv", index=False)
conv_matlab_to_csv('assets/myMat2.mat')
df = pd.read_csv('mat3_converted_to_csv.csv')
fig = px.line(df, y='DOAmean')
app.layout = html.Div([
dcc.Graph(figure=fig)
])
if __name__=='__main__':
app.run(port=8002)