Home > Article > Backend Development > Let's talk about how to read the contents of mat files (matlab data) using python
How to read the contents of mat file using python? The following article will introduce to you the method of reading matlab data (.mat file) in Python. I hope it will be helpful to you!
We all know that matlab is a very easy-to-use matrix calculation and analysis software. However, the drawing effect of matlab is extremely jagged, so here is a python Read the data .mat file processed by matlab.
1. First, we open matlab here and enter two variables in the command line window.
matlab_x=1:0.01:10; matlab_y=sin(matlab_x);
2. After calculation and processing, there will be two variable values in the workspace on the right side of matlab , respectively matlab_y, matlab_x
3. Then, we place the mouse in the empty position of the workspace and right-click, selectSave , you can also use the shortcut key ctrl s to save when the workspace is in the highlighted state, and the save file name will pop up. Here we save it as matlab.mat
4. The next step is to use Python to read the data Data of the matlab workspace saved in the previous step. In Python, we need to use the scipy library. Here we import it first
import scipy.io as scio
5. Read the .mat file
data=scio.loadmat('./matlab.mat')
6. View the current data data type
type(data)
Output is a dict dictionary type
7. Read the data corresponding to what we want
Here we assume that we need to read the data matlab_y into python (here we use the numpy library to convert the data into an array type )
import numpy as np #导入矩阵处理库 python_y=np.array(data['matlab_y']) #将matlab数据赋值给python变量
At this point, reading matlab data using python is completed.
enjoy it!
【Related recommendations: Python3 video tutorial】
The above is the detailed content of Let's talk about how to read the contents of mat files (matlab data) using python. For more information, please follow other related articles on the PHP Chinese website!