Importing matlab .mat in spyder vs importing .mat using scipy.io

1,000 views
Skip to first unread message

egayer

unread,
Jan 16, 2014, 7:48:43 AM1/16/14
to spyd...@googlegroups.com
Hi, 

I am wondering how spider manages to import .mat file directly into the variable explorer (by double clicking on the .mat file) while the content of a .mat file is nested when using scipy.io.loadmat

for example to load a mat file 'dummy.mat' that contains 2 matrix A and B :

1) I double click on the file from the spider file explorer and A and B will be added in the variable explorer

2) using scipy.io I do:

 x=scipy.io.loadmat('dummy.mat')

and A and B are nested into x

thanks for your help
Eric

David Verelst

unread,
Jan 16, 2014, 3:45:04 PM1/16/14
to spyder

Hi,

Do you mind sharing a little bit of information regarding your setup, such as Spyder version and OS? That helps us to understand what is happening on your end since Spyder has seen a lot of changes the last few months.

The importing of Matlab variables is done in spyderlib/utils/iofuncs.py. You can browse through the current version here, or look at the version from Stevens branch here (there is pull request on that here). The latter holds certain improvements that have not yet been merged with the main Spyder development branch.

A small example (taken from iofuncs.py from Stevens branch) on how you could import Matlab variables using a script:

from spyderlib.utils.iofuncs import get_matlab_value
import scipy.io as spio

out = spio.loadmat(filename)
for key, value in list(out.items()):
    out[key] = get_matlab_value(value)
# now you can access the variable(s) you saved in the matlab file as follows
matlab_var = out['matlab_var_name']
# all matlab variables are nested under matlab_var and are accessible as a dictionaries
Unfortunately there isn't yet an easy one-stop function to import a Matlab .mat file with exactly the same behaviour as in Matlab. As it currently stands now, a users is required to "browse" a bit through the output of spio.loadmat, and that can be a bit cumbersome. The MatlabStruct class in iofuncs.py from Steven improves the situation over the default scipy.io.loadmat behaviour.

Regards,
David


--
You received this message because you are subscribed to the Google Groups "spyder" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spyderlib+...@googlegroups.com.
To post to this group, send email to spyd...@googlegroups.com.
Visit this group at http://groups.google.com/group/spyderlib.
For more options, visit https://groups.google.com/groups/opt_out.

David Verelst

unread,
Jan 16, 2014, 4:00:40 PM1/16/14
to spyder
Ok, that might not have been very helpful and doesn't exactly answers your question...

I am not sure how Spyder is doing it exactly, and I can't figure out an equivalent of a single call to a function that will replicate this behaviour from a script. It think the magic is happening in spyderlib/widgets/externalshell/monitor.py and/or namespacebrowser.py. Maybe that could be a feature request?

Regards,
David
Reply all
Reply to author
Forward
0 new messages