Let's talk about how to read the contents of mat files (matlab data) using python

青灯夜游
Release: 2022-09-30 18:17:31
forward
7945 people have browsed it

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!

Let's talk about how to read the contents of mat files (matlab data) using python

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);
Copy after login

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

Lets talk about how to read the contents of mat files (matlab data) using python

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
Copy after login

5. Read the .mat file

data=scio.loadmat('./matlab.mat')
Copy after login

6. View the current data data type

type(data)
Copy after login

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变量
Copy after login

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template