SciPy – 58 – File IO – interagire con MATLAB-Octave

Continuo da qui, copio qui.

Interagire con MATLAB / Octave
Faccio riferimento a Octave, ha tutto quel che serve (e anche di più) della versione e$o$a.

loadmat(file_name[, mdict, appendmat]) load MATLAB file.
savemat(file_name, mdict[, appendmat, ...]) save a dictionary of names and arrays into a MATLAB-style .mat file.
whosmat(file_name[, appendmat]) list variables inside a MATLAB file.

Le funzioni base
We’ll start by importing scipy.io and calling it sio for convenience:

If you are using IPython, try tab completing on sio. Among the many options, you will find:

in particolare:

sio.loadmat
sio.savemat
sio.whosmat

These are the high-level functions you will most likely use when working with MATLAB files. You’ll also find:

sio.matlab

This is the package from which loadmat, savemat and whosmat are imported. Within sio.matlab, you will find the mio module This module contains the machinery that loadmat and savemat use. From time to time you may find yourself re-using this machinery.

Come partire
You may have a .mat file that you want to read into Scipy. Or, you want to pass some variables from Scipy / Numpy into MATLAB.

To save us using a MATLAB license, let’s start in Octave. Octave has MATLAB-compatible save and load functions. Start Octave (octave at the command line):

 

Now, to Python:

Now let’s try the other way round:

Then back to Octave:

If you want to inspect the contents of a MATLAB file without reading the data into memory, use the whosmat command:

whosmat returns a list of tuples, one for each array (or other object) in the file. Each tuple contains the name, shape and data type of the array.

Structs di MATLAB
MATLAB structs are a little bit like Python dicts, except the field names must be strings. Any MATLAB object can be a value of a field. As for all objects in MATLAB, structs are in fact arrays of structs, where a single struct is an array of shape (1, 1).

We can load this in Python:

In versions of Scipy from 0.12.0, MATLAB structs come back as numpy structured arrays, with fields named for the struct fields. You can see the field names in the dtype output above. Note also:

and

So, in MATLAB, the struct array must be at least 2D, and we replicate that when we read into Scipy. If you want all length 1 dimensions squeezed out, try this:

Sometimes, it’s more convenient to load the MATLAB structs as python objects rather than numpy structured arrays – it can make the access syntax in Python a bit more similar to that in MATLAB. In order to do this, use the struct_as_record=False parameter setting to loadmat.

struct_as_record=False works nicely with squeeze_me:

Saving struct arrays can be done in various ways. One simple method is to use dicts:

loaded as:

You can also save structs back again to MATLAB (or Octave in our case) like this:

Arrays di celle MATLAB
Cell arrays in MATLAB are rather like python lists, in the sense that the elements in the arrays can contain any type of MATLAB object. In fact they are most similar to numpy object arrays, and that is how we load them into numpy.

Back to Python:

Saving to a MATLAB cell array just involves making a numpy object array:



Come si vede SciPy e Octave (MATLAB) possono dialogare, anche se non credo sia roba da tutti i giorni.

A questo punto si segue con altri programmi, IDL per esempio, ma non solo. Salto. E mi sa che devo cominciare a pensare a qualche nota di conclusioni personali.

:mrgreen:

Posta un commento o usa questo indirizzo per il trackback.

Trackback

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo di WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione /  Modifica )

Google photo

Stai commentando usando il tuo account Google. Chiudi sessione /  Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione /  Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione /  Modifica )

Connessione a %s...

Questo sito utilizza Akismet per ridurre lo spam. Scopri come vengono elaborati i dati derivati dai commenti.

%d blogger hanno fatto clic su Mi Piace per questo: