How to extract data from Matlab .fig files in Python? - python

If I have some X vs Y data saved in a Matlab .fig file, is there a way to extract that data in Python? I've tried using the method shown in a previous discussion, but this does not work for me. I have also tried to open the files using h5py and PyTables, since .mat files are actually HDF5 files now, but this results in an error where a valid file signature can't be found.
Currently I'm trying to do this with the Anaconda distribution of Python 3.4.
EDIT: I managed to figure out something that works, but I don't know why. This has me worried something might break in the future and I won't be able to debug it. If anyone can explain why this works, but the method in the old discussion doesn't I'd really appreciate it.
from scipy.io import loadmat
d = loadmat('linear.fig', squeeze_me=True, struct_as_record=False)
x = d['hgS_070000'].children.children.properties.XData
y = d['hgS_070000'].children.children.properties.YData

The best way I can think of is using any of the Matlab-Python bridge (such as pymatbridge).
You can call Matlab code directly on python files and transform the data from one to the other. You could use some Matlab code to load the fig and extract the data and then convert the numerical variables to python arrays (or numpy arrays) easily.

Related

How to read an .h5 file from xmsd2 using Python correctly?

I am using the xmds2 software and its outputs seems to be an h5 file.
I would like to use python (spyder) software to make possible to plot them into a 2d and 3d graphs.
The problem is that I can't realize how do it at all.
The code I run in Spyder is the following:
import numpy as np
import h5py
with h5py.File(r'Path...\lorenz.h5','r') as hdf:
ls=list(hdf.keys())
print('List of datasets in this file: \n', ls)
the only output I get is
['1']
So I would think that there is a column or something whose name is '1'
The question here is that my program in xmds2 outputs xR,yR,zR (and time I guess) non anything called '1' So I am really confused about what to do in order plot or load them into my python program correctly
I am newbie at using xmds2 so if someone could help me I would be grateful. And also this is the first time I have to deal with an .h5 file.
The program I run in xmds2 is here
http://www.xmds.org/tutorial.html
Thanks for your help!

numpy python 3.4 for beginners

I am a beginner in programming.
I installed anaconda on my laptop in order to get numpy. After that I load the npy files as follows:
import numpy
X_train = numpy.load("train-features.npy")
X_test = numpy.load("test-features.npy")
Now I would like to see what there is inside them. So i tried to print them but it gives me a memory error.
How can I look into these file in order to understand how my data set looks like?
Without knowing details or error, I am assuming that you may be getting memory error due to memory buffer getting overwhelmed because of large size of your data.
If that is the case, then you may want to memory map the npy / dat file to a disk.
Detailed examples are available at scipy.org numpy.load page
and details about numpy.memmap are available here
If you can post error and provide more insight into data that may help one with determining correct cause of the problem.

Move data from a MATLAB program to python

I am fairly new to programming, and started programming in python 3. The data I want to analyze have already been processed in a matlab program, and I need to export them. I don't know anything about matlab, and after searching the web I came up with this:
fileID = fopen('VarA.txt','w');
fprintf(fileID,'%.10f \n',data_1(:,1));
fclose(fileID);
fileID = fopen('varB.txt','w');
fprintf(fileID,'%.10f \n',data_1(:,2));
fclose(fileID);
This gives me 2 .txt files with x and y coordinates respectively. I have about 1000 strings (which each contains about 10k datapoints), so this seems like an awful way to do this.
My question is then; how can I export these datasets with an efficient code? For instance: I am writing a dataset to 2 different .txt files which separates the 2 variables, instead of a hash saved in 1 file.
If you are interested in exporting a lot of structured numerical data, i recommend you use the HDF5 functions in matlab to write these, and the corresponding python library to read these.
To simplify the code you showed there, read about dlmwrite in the matlab help.
The way you choose (or via dlmwrite) may sound awful in the beginning, but very often will not really have an impact on the performance.

Any way to get a figure from Python's matplotlib into Matlab?

I'm processing some data for a research project, and I'm writing all my scripts in python. I've been using matplotlib to create graphs to present to my supervisor. However, he is a die-hard MATLAB user and he wants me to send him MATLAB .fig files rather than SVG images.
I've looked all over but can't find anything to do the job. Is there any way to either export .fig files from matplotlib, convert .svg files to .fig, or import .svg files into MATLAB?
Without access to (or experience with matlab) this is going to be a bit tricky. As Amro stated, .fig files store the underlying data, and not just an image, and you're going to have a hard time saving .fig files from python. There are however a couple of things which might work in your favour, these are:
numpy/scipy can read and write matlab .mat files
the matplotlib plotting commands are very similar to/ based on the matlab ones, so the code to generate plots from the data is going to be nearly identical (modulo round/square brackets and 0/1 based indexing).
My approach would be to write your data out as .mat files, and then just put your plotting commands in a script and give that to your supervisor - with any luck it shouldn't be too hard for him to recreate the plots based on that information.
If you had access to Matlab to test/debug, I'm sure it would be possible to create some code which automagically created .mat files and a matlab .m file which would recreate the figures.
There's a neat list of matlab/scipy equivalent commands on the scipy web site.
good luck!

how to view sparse matrices outside python environment

I am working with sparse matrices which are 11685 by 85730 . I am able to store it only as a .pickle file . I want to view the file outside the python environment also . I tried saving as a .txt and .csv files but they are of no help . Can anybody suggest a suitable format and library so that I can view those matrices outside the python environment .
Python allows you to write to many formats that are readable outside of python. .csv is one format, but there are also HDF5 and netcdf4 among others (those are meant to store array data though).
http://code.google.com/p/netcdf4-python/
http://code.google.com/p/h5py/
Or you could save them in a matlab readable format:
http://docs.scipy.org/doc/scipy/reference/tutorial/io.html
What you use should depend on how you plan on accessing the data outside of python.

Categories

Resources