Scipy provides routines to read and write Matlab mat files. As an example let's make a program where we create a Matlab compatible file storing a (1x11) matrix, and then read this data into a numpy array from Python using the scipy Input-Output library:
First we create a mat file in Octave:
octave :1> a = -1:0.5:4
a =
Columns 1 through 6:
-1.0000 -0.5000 0.0000 0.5000 1.0000 1.5000
Columns 7 through 11:
2.0000 2.5000 3.0000 3.5000 4.0000
octave :2> save -6 octave_a .mat a % save as version 6
Then we load this array within python:
from scipy .io import loadmat
mat_contents = loadmat ('octave_a .mat ')
print(mat_contents)
print('\n')
print(mat_contents ['a'])
The output of the above program is shown below:
{' __globals__ ': [],
'__header__ ': 'MATLAB 5.0 MAT -file , Octave 3.2 , 2011 -09 -16 22:13:21 ',
'__version__ ': '1.0 ',
'a': array ([[ -1. , -0.5, 0. , 0.5 , 1. , 1.5 , 2. , 2.5 ,\
3. , 3.5 , 4. ]])}
([[ -1. , -0.5, 0. , 0.5 , 1. , 1.5 , 2. , 2.5 , \
3. , 3.5 , 4. ]])
Press any key to continue . . .
A Matlab matrix file can hold several arrays. Each of those is presented by one key-value pair in
the dictionary. Let's make another program which saves two arrays from Python to demonstrate that:
# create two numpy arrays
a = np. linspace (0, 50, 11)
b = np. ones ((4 , 4))
# save as mat - file
# create dictionary for savemat
tmp_d = {'a': a,'b': b}
The output of the above program is shown below:
First we create a mat file in Octave:
octave :1> a = -1:0.5:4
a =
Columns 1 through 6:
-1.0000 -0.5000 0.0000 0.5000 1.0000 1.5000
Columns 7 through 11:
2.0000 2.5000 3.0000 3.5000 4.0000
octave :2> save -6 octave_a .mat a % save as version 6
Then we load this array within python:
from scipy .io import loadmat
mat_contents = loadmat ('octave_a .mat ')
print(mat_contents)
print('\n')
print(mat_contents ['a'])
The output of the above program is shown below:
{' __globals__ ': [],
'__header__ ': 'MATLAB 5.0 MAT -file , Octave 3.2 , 2011 -09 -16 22:13:21 ',
'__version__ ': '1.0 ',
'a': array ([[ -1. , -0.5, 0. , 0.5 , 1. , 1.5 , 2. , 2.5 ,\
3. , 3.5 , 4. ]])}
([[ -1. , -0.5, 0. , 0.5 , 1. , 1.5 , 2. , 2.5 , \
3. , 3.5 , 4. ]])
------------------
(program exited with code: 0)
Press any key to continue . . .
The function loadmat returns a dictionary: the key for each item in the dictionary is a string which
is the name of that array when it was saved in Matlab. The key is the actual array.
is the name of that array when it was saved in Matlab. The key is the actual array.
A Matlab matrix file can hold several arrays. Each of those is presented by one key-value pair in
the dictionary. Let's make another program which saves two arrays from Python to demonstrate that:
import scipy .io
import numpy as np
import numpy as np
# create two numpy arrays
a = np. linspace (0, 50, 11)
b = np. ones ((4 , 4))
# save as mat - file
# create dictionary for savemat
tmp_d = {'a': a,'b': b}
scipy .io. savemat ('data .mat ', tmp_d )
The output of the above program is shown below:
HAL47 : code fangohr$ octave
GNU Octave , version 3.2.4
Copyright (C) 2009 John W. Eaton and others .
<snip >
octave :1> whos
Variables in the current scope :
Attr Name Size Bytes Class
==== ==== ==== ===== =====
ans 1x11 92 cell
Total is 11 elements using 92 bytes
octave :2> load data .mat
octave :3> whos
Variables in the current scope :
Attr Name Size Bytes Class
==== ==== ==== ===== =====
a 11 x1 88 double
ans 1x11 92 cell
b 4x4 128 double
Total is 38 elements using 308 bytes
octave :4> a
a =
0
5
10
15
20
25
30
35
40
45
50
octave :5> b
b = 1 1 1 1
GNU Octave , version 3.2.4
Copyright (C) 2009 John W. Eaton and others .
<snip >
octave :1> whos
Variables in the current scope :
Attr Name Size Bytes Class
==== ==== ==== ===== =====
ans 1x11 92 cell
Total is 11 elements using 92 bytes
octave :2> load data .mat
octave :3> whos
Variables in the current scope :
Attr Name Size Bytes Class
==== ==== ==== ===== =====
a 11 x1 88 double
ans 1x11 92 cell
b 4x4 128 double
Total is 38 elements using 308 bytes
octave :4> a
a =
0
5
10
15
20
25
30
35
40
45
50
octave :5> b
b = 1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
The program creates the file data.mat, which we can subsequently read using Matlab or here Octave. There are other functions to read from and write to in formats as used by IDL, Netcdf and other formats in scipy.io.
This is the end of today's discussion. We shall start with pandas in the next post. Till we meet next keep practicing and learning Python as Python is easy to learn!
This is the end of today's discussion. We shall start with pandas in the next post. Till we meet next keep practicing and learning Python as Python is easy to learn!
0 comments:
Post a Comment