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 Python manipulates Matlab data in Mat format

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces Python how to operate Matlab Mat format data related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this Python how to operate Matlab Mat format data article will have a harvest, let's take a look.

1. Non-matlab v7.3 files read / write import scipy.io as sioimport numpy# matFile read matFile = 'matlabdata.mat'datas = sio.loadmat (matFile) # load data in matFile # suppose the variable stored in mat is matlabdatamatlabdata = datas [' matlabdata'] # matFile write

Save_matFile = 'save_matlabdata.mat'

Save_matlabdata = np.array ([1, 2, 3, 4, 5])

Sio.savemat (save_matFile, {'array':save_matlabdata}) 2. Matlab v7.3 files read

If matlab saves data, there will be an error when loading data using the'- v7.3recording journal journal scipy.io.loadmat function:

File "/ usr/local/lib/python2.7/dist-packages/scipy/io/matlab/mio.py", line 64, in mat_reader_factory

Raise NotImplementedError ('Please use HDF reader for matlab v7.3 files')

NotImplementedError: Please use HDF reader for matlab v7.3 files

You can use:

Import h6pywith h6py.File ('matlabdata.mat', 'r') as f: variable name datas = h6py.File (' matlabdata.mat') ['matlabdata'] .value in f.keys () # matlabdata.mat

Add: [Matlab/Python] data transfer between Matlab and Python

Most of the time, we need to save the data in matlab and process it with python.

Method 1 (.mat format)

Directly store the data of matlab into .mat format, and then use the loadmat function in scipy.io to read it in python.

Import scipy.io as siodata = sio.loadmat (...) Method 2 (.h6 format)

When the .mat file to be stored is large, you need to use save-v7.3 in matlab to store it. But when using method 1 to read in python, it will not be supported. Can be read in the following ways

With h6py.File ("mydata.mat") as f: data = f ["mydata"] [:]

The data can be read correctly, but the array dimension will be reversed, that is, it should be (2meme, 3pr, 4p5), and it will be (5meme, 4jol, 3p2).

But only when the .mat data is saved with save-v7.3 can it be read by this method, otherwise, the application method will read the

So you can save the data in matlab to .h6 format and read it according to method 2.

The h6 format is stored in matlab in the following way

H6create ('data.h6','/data',; data = rand); h6write (' data.h6','/data',data)

But when reading with python, the matrix dimension is still reversed.

This is the end of the article on "how Python manipulates Matlab's Mat format data". Thank you for reading! I believe you all have a certain understanding of "how Python operates Matlab's Mat format data". If you want to learn more, you are welcome to follow the industry information channel.

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

Development

Wechat

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

12
Report