Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to read and write .mat files in matlab by Python

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how Python reads and writes .mat files in matlab. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Background

In the process of doing deeplearning, use caffe framework, generally use matlab to deal with pictures (matlab processing pictures is relatively simple, efficient), use python to generate the required lmdb files and do test to produce results. So some matlab from the image processing label information will be .mat file for python to read, but also the result information generated by python also need matlab to do further processing (of course, you can also use txt, do not bother to deal with structural information).

Introduction

The data transfer between matlab and python is generally based on the file format of matlab. Mat, numpy and scipy in python provide some functions that can read, write and process the data of .mat file.

Here, the role of numpy is to provide Array functions to map the Matrix in matlab, while scipy provides two functions, loadmat and savemat, to read and write .mat files.

Example import scipy.io as sioimport numpy as np### below is an explanation of how python reads .mat files and how to handle the result # load_fn = 'xxx.mat'load_data = sio.loadmat (load_fn) load_matrix = load_data [' matrix'] # assuming that the character variable stored in the file is matrix, such as save (load_fn, 'matrix') in matlab Of course, you can save multiple save (load_fn, 'matrix_x',' matrix_y',...) Load_matrix_row = load_matrix [0] # take the first line of matrix in matlab at that time, and arrange the array lines in python # below is to explain how python saves .mat files for matlab programs to use # save_fn = 'xxx.mat'save_array = np.array) sio.savemat (save_fn, {' array': save_array}) # as above The first line of the array variable, save_array_x = np.array ([1min2pence3je 4]) save_array_y = np.array ([5mae6pje 7je 8]) sio.savemat (save_fn, {'array_x': save_array_x,' array_y': save_array_y}) # is the same, but two different variables are saved for use, and that's all for the article on "how Python reads and writes .mat files in matlab". Hope that the above content can be helpful to you, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report