How to read and write and store matlab data files in python (*. mat), python. mat

Source: Internet
Author: User

How to read and write and store matlab data files in python (*. mat), python. mat

Background

In the deeplearning process, caffe framework is used, and matlab is generally used to process images (matlab is relatively simple and efficient to process images ), use python to generate the required lmdb file and perform test to generate the result. Therefore, some label information obtained by matlab from image processing will use. the mat file is for python to read, and the result information generated by python also needs matlab for further processing (of course, you can also use txt, so you don't need to handle the structure information yourself ).

Introduction

Data transmission between matlab and python is generally based on the matlab file format. in mat and python, numpy and scipy provide some functions, which can be very good. the data in the mat file is read, written, and processed.

Numpy provides the Array Function to map the Matrix in matlab, while scipy provides two functions: loadmat and savemat to read and write the. mat file.

The following is a simple test program. For more information about function usage, see the help document:

Import scipy. io as SiO2 import matplotlib. pyplot as plt import numpy as np # matlab file name matfn = u'e:/python/Test Program/16225067213162251656_1244.mat 'data = sio. loadmat (matfn) plt. close ('all') xi = data ['xi'] yi = data ['yi'] ui = data ['ui'] vi = data ['vi'] plt. figure (1) plt. quiver (xi [: 5,: 5], yi [: 5,: 5], ui [: 5,: 5], vi [:: 5,: 5]) plt. figure (2) plt. partition F (xi, yi, ui) plt. show () sio. savemat ('saveddata. mat ', {'Xi': xi, 'yi': yi, 'ui': ui, 'vi': vi })

Example 2

Import scipy. io as sioimport numpy as np ### The following describes how to read python. mat file and how to handle the result ### load_fn = 'xxx. mat 'Load _ data = sio. loadmat (load_fn) load_matrix = load_data ['matrix '] # assume that the character variable in the file is matrix. For example, 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] # obtain the first row of matrix in matlab at that time, and arrange the array rows in python ### The following describes how to save python. the mat file is used by the matlab program ### save_fn = 'xxx. mat 'Save _ array = np. array ([1, 2, 3, 4]) sio. savemat (save_fn, {'array': save_array}) # Like above, the first row of the array variable save_array_x = np exists. array ([1, 2, 3, 4]) save_array_y = np. array ([,]) sio. savemat (save_fn, {'array _ x': save_array_x, 'array _ x': save_array_x}) # Similarly, only two different variables are saved

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.