Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create .mat v7.3 files from Python using hdf5storage

Tags:

python

hdf5

mat

Is there any way to create .mat v7.3 files from Python? I have been managed to create hdf5 files from Python but not in converting them into .mat v7.3 files. I found a package called hdf5storage but I don't know how to use it.

like image 237
Peter Avatar asked Jul 05 '26 05:07

Peter


1 Answers

Example (requires Python >= 2.6 with the NumPy and h5py >= 2.1 packages):

import hdf5storage # get code on https://pypi.python.org/pypi/hdf5storage/0.1.3

matcontent = {}
matcontent[u'some_numbers'] = [10, 50, 20] # each key must be a unicode string
hdf5storage.write(matcontent, '.', 'test.mat', store_python_metadata=False, matlab_compatible=True)

In Matlab:

enter image description here


If you want to have a matrix instead of a cell array in Matlab:

import hdf5storage # get code on https://pypi.python.org/pypi/hdf5storage/0.1.3
import numpy as np

matcontent = {}
matcontent[u'some_numbers'] = np.array([10, 50, 20]) # each key must be a unicode string
hdf5storage.write(matcontent, '.', 'test2.mat', matlab_compatible=True)

enter image description here

As you will notice, the second example is much faster (>x10).


More generally, refer to the documentation for the data storage matching:

enter image description here

like image 153
Franck Dernoncourt Avatar answered Jul 06 '26 18:07

Franck Dernoncourt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!