15.12.2012 Views

scipy tutorial - Baustatik-Info-Server

scipy tutorial - Baustatik-Info-Server

scipy tutorial - Baustatik-Info-Server

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

SciPy Reference Guide, Release 0.8.dev<br />

loaded as:<br />

octave:21> load saved_struct<br />

octave:22> a_dict<br />

a_dict =<br />

{<br />

field2 = a string<br />

field1 = 0.50000<br />

}<br />

Further up, you’ll remember this deprecation warning:<br />

>>> mat_contents = sio.loadmat(’octave_a.mat’)<br />

/home/mb312/usr/local/lib/python2.5/site-packages/<strong>scipy</strong>/io/Matlab/mio.py:84: FutureWarning: Using str<br />

return MatFile5Reader(byte_stream, **kwargs)<br />

The way that the reader returns struct arrays will soon change. Like this:<br />

>>> mat_contents = sio.loadmat(’octave_struct.mat’, struct_as_record=True)<br />

>>> oct_struct = mat_contents[’my_struct’]<br />

>>> val = oct_struct[0,0]<br />

>>> print val<br />

([[1.0]], [[2.0]])<br />

>>> print val.dtype<br />

[(’field1’, ’|O8’), (’field2’, ’|O8’)]<br />

You can also save structs back again to Matlab (or Octave in our case) like this:<br />

>>> dt = [(’f1’, ’f8’), (’f2’, ’S10’)]<br />

>>> arr = np.zeros((2,), dtype=dt)<br />

>>> print arr<br />

[(0.0, ’’) (0.0, ’’)]<br />

>>> arr[0][’f1’] = 0.5<br />

>>> arr[0][’f2’] = ’python’<br />

>>> arr[1][’f1’] = 99<br />

>>> arr[1][’f2’] = ’not perl’<br />

>>> sio.savemat(’np_struct_arr.mat’, {’arr’: arr})<br />

Matlab cell arrays<br />

Cell arrays in Matlab are rather like python lists, in the sense that the elements in the arrays can contain any type of<br />

Matlab object. In fact they are most similar to numpy object arrays, and that is how we load them into numpy.<br />

octave:14> my_cells = {1, [2, 3]}<br />

my_cells =<br />

{<br />

}<br />

[1,1] = 1<br />

[1,2] =<br />

2 3<br />

octave:15> save -6 octave_cells.mat my_cells<br />

84 Chapter 1. SciPy Tutorial

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!