Differences between revisions 4 and 5

Deletions are marked like this. | Additions are marked like this. |

Line 6: | Line 6: |

Line 9: | Line 8: |

{{{#!python |
{{{ #!python |

Line 11: | Line 11: |

from scipy.io import mio x = mio.loadmat('test.mat') |
from scipy.io import loadmat x = loadmat('test.mat') |

Line 16: | Line 15: |

Line 18: | Line 16: |

lon = mio.loadmat('test.mat')['lon'] |
lon = loadmat('test.mat')['lon'] }}} = Matlab 7.3 and greater = Beginning at release 7.3 of Matlab, mat files are actually saved using the HDF5 format by default (except if you use the -vX flag at save time, see {{{help save}}} in Matlab). These files can be read in Python using, for instance, the [http://www.pytables.org/moin PyTables] or [http://h5py.alfven.org/ h5py] package. Reading Matlab structures in mat files does not seem supported at this point. |

Line 20: | Line 21: |

}}} = Matlab 7.3 and greater = Beginning at release 7.3 of Matlab, mat files are actually saved using the HDF5 format by default (except if you use the -vX flag at save time, see {{{help save}}} in Matlab). These files can be read in Python using, for instance, the [http://www.pytables.org/moin PyTables] package. Reading Matlab structures in mat files does not seem supported at this point. {{{#!python |
{{{ #!python |

Line 29: | Line 24: |

Line 34: | Line 28: |

Line 38: | Line 31: |

`mat` files contain data saved in Matlab's proprietary format. How to read these files in Python depends on the version of Matlab used to save them, up to 7.1 or greater.

Here are exemples of how to read two variables `lat` and `lon` from a mat file called "test.mat".

# Matlab up to 7.1

mat files created with Matlab up to version 7.1 can be read using the `mio` module part of `scipy.io`. Reading structures (and arrays of structures) is supported, elements are accessed with the same syntax as in Matlab: after reading a structure called e.g. `struct`, its `lat` element can be obtained with `struct.lat`, or `struct.__getattribute__('lat')` if the element name comes from a string.

```
1 #!/usr/bin/env python
2 from scipy.io import loadmat
3 x = loadmat('test.mat')
4 lon = x['lon']
5 lat = x['lat']
6 # one-liner to read a single variable
7 lon = loadmat('test.mat')['lon']
```

# Matlab 7.3 and greater

Beginning at release 7.3 of Matlab, mat files are actually saved using the HDF5 format by default (except if you use the -vX flag at save time, see `help save` in Matlab). These files can be read in Python using, for instance, the PyTables or h5py package. Reading Matlab structures in mat files does not seem supported at this point.

```
1 #!/usr/bin/env python
2 import tables
3 file = tables.openFile('test.mat')
4 lon = file.root.lon[:]
5 lat = file.root.lat[:]
6 # Alternate syntax if the variable name is in a string
7 varname = 'lon'
8 lon = file.getNode('/' + varname)[:]
```