List matlab files variables (scipy.io.matlab) #121

Closed
wants to merge 1 commit into
from

Projects

None yet

5 participants

@loluengo
Contributor

The new function whosmat tries to resemble the matlab 'whos' command. At the moment it gives a list of (name,shape) tuples.

I thought of writing this, to avoid loading the whole .mat file to know the names of variables, sometimes the .mat files can be really huge.

Regards!

Owner

Looks good.

I'm wondering about the <7.3 support (in general, not just this function), because fewer and fewer people will have a Matlab from before 2006. It may be useful to support more recent versions in the same functions with an optional dependency on pytables or h5py. Do you happen to know how much work that would be? Presumably it's a lot easier for hdf5 files.

Contributor

It's not that true. Currently I'm running a quite recent Matlab
version, and by default it saves as a v7 formatted .mat file. So it
seems that this format is not going away soon.

But it would also be great to have v7.3 support. Maybe I will be trying
something like that in a while.

Regards!

On 12/28/2011 03:26 PM, Ralf Gommers wrote:

Looks good.

I'm wondering about the<7.3 support (in general, not just this function), because fewer and fewer people will have a Matlab from before 2006. It may be useful to support more recent versions in the same functions with an optional dependency on pytables or h5py. Do you happen to know how much work that would be? Presumably it's a lot easier for hdf5 files.


Reply to this email directly or view it on GitHub:
#121 (comment)

Owner

Ah, I didn't realize that Matlab saves in an older version by default. I'm still stuck on 2003a, but will soon get a recent version.

Member

This is a great idea. I added a few line comments. I tested it on a couple trivial examples, and it worked fine, but it needs unit tests. There are sample Matlab files in scipy/scipy/io/matlab/tests/data that might be useful for this.

Member

Can an indication of what the object is be included in the return value? Not every variable in a .mat file is an array. For example, consider the file 'some_functions.mat' from the io/matlab/tests/data directory:

In [1]: from scipy.io import loadmat, whosmat

In [2]: whosmat('some_functions.mat')
Out[2]: 
[('a', [1, 1]),
 ('b', [1, 1]),
 ('c', [1, 1]),
 ('sqr', [1, 1]),
 ('parabola', [1, 1]),
 ('nCf', [1, 1]),
 ('__function_workspace__', [1, 1408])]

In [3]: loadmat('some_functions.mat')
Out[3]: 
{'__function_workspace__': array([[ 0,  1, 73, ...,  0,  0,  0]], dtype=uint8),
 '__globals__': [],
 '__header__': 'MATLAB 5.0 MAT-file, Platform: GLNXA64, Created on: Tue Jan  5 08:01:15 2010',
 '__version__': '1.0',
 'a': array([[-3.9]]),
 'b': array([[52]], dtype=uint8),
 'c': array([[0]], dtype=uint8),
 'nCf': MatlabFunction([[ ([u'/opt/matlab-2009a'], [u'/'], [u'@'], [[(array([u'nCountFun'], 
      dtype='<U9'), array([u'simple'], 
      dtype='<U6'), array([u'/home/mb312/tmp/nCountFun.m'], 
      dtype='<U27'))]])]], 
      dtype=[('matlabroot', '|O4'), ('separator', '|O4'), ('sentinel', '|O4'), ('function_handle', '|O4')]),
 'parabola': MatlabFunction([[ ([u'/opt/matlab-2009a'], [u'/'], [u'@'], [[(array([u'sf%0@(x)a*x.^2+b*x+c'], 
      dtype='<U20'), array([u'anonymous'], 
      dtype='<U9'), array([], 
      dtype='<U1'), MatlabOpaque([ ('', 'MCOS', 'function_handle_workspace', [[3707764736L], [2L], [1L], [1L], [2L], [1L]])], 
      dtype=[('s0', '|O4'), ('s1', '|O4'), ('s2', '|O4'), ('arr', '|O4')]))]])]], 
      dtype=[('matlabroot', '|O4'), ('separator', '|O4'), ('sentinel', '|O4'), ('function_handle', '|O4')]),
 'sqr': MatlabFunction([[ ([u'/opt/matlab-2009a'], [u'/'], [u'@'], [[(array([u'sf%0@(x)x.^2'], 
      dtype='<U12'), array([u'anonymous'], 
      dtype='<U9'), array([], 
      dtype='<U1'), MatlabOpaque([ ('', 'MCOS', 'function_handle_workspace', [[3707764736L], [2L], [1L], [1L], [1L], [1L]])], 
      dtype=[('s0', '|O4'), ('s1', '|O4'), ('s2', '|O4'), ('arr', '|O4')]))]])]], 
      dtype=[('matlabroot', '|O4'), ('separator', '|O4'), ('sentinel', '|O4'), ('function_handle', '|O4')])}

If it can be done without reading the entire file, it would be great if the result of whosmat could show that, for example, 'sqr', 'parabola' and 'nCf' are functions.

Owner

Hi @loluengo, are you planning to address Warren's comments at some point? It would be nice to include this in the next release (which should come out in a month or so).

Owner
pv commented Oct 21, 2012

All comments addressed + bugs fixed here:
https://github.com/pv/scipy-work/commits/oth/loluengo/matlab-listvars

Merge-ready?

Owner

@matthew-brett do you want to take a look at this?

Contributor

Looks good to me - thanks very much for the work.

Owner
pv commented Nov 8, 2012

Thanks, merged.

@pv pv closed this Nov 8, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment