Getting a matlab code's results on python - python

I have code in matlab whose results I would like to use in python code (as a matrix or as a .dat file). Could anyone tell me how this could be done?

NumPy and SciPy can read and write MATLAB .mat files directly using scipy.io.loadmat and scipy.io.savemat.

A .dat file is just a text file:
fid = fopen('outputFile.dat', 'wt');
fprintf(fid, your_data_in_string_format);
fclose(fid);

Related

How to write multiple matrix into excel in python

I have a .mat file that I opened it in python. It's a (66365) matrix. I want to write this to a Excel file. How can I di it?
I open it by following code:
import scipy.io
mat= scipy.io.loadmat('2003.mat')
You can either use the builtin csv library:
https://docs.python.org/3.4/library/csv.html
or use NumPy instead using this method:
Dump a NumPy array into a csv file
I generally recommend NumPy, but I don't think there is a significant difference here.

Error in MATLAB when opening .mat files with Chinese characters written by savemat

It looks like savemat is producing .mat files that MATLAB cannot open when they contain Chinese characters, but loadmat is able to:
$ python -c 'from scipy.io import *; savemat("tmp.mat", {"test": "你好"}); print(loadmat("tmp.mat")["test"])' && matlab -nodesktop -r 'load tmp; exit'
['你好']
To get started, type one of these: helpwin, helpdesk, or demo.
For product information, visit www.mathworks.com.
Error using load
Can't read file /home/tmp.mat.
Any ideas how I can either change savemat to output something MATLAB can open or change MATLAB settings such that I can read the files?
Based on the documentation of savemat and save I believe that savemat produces .mat files with an older format (what MATLAB calls -v6, that doesn't support Unicode (introduced in -v7).
You should be able to save .mat files of the newer format if you switch from savemat to an HDF5 writer like h5py or hd5storage. Alternatively, you can always store strings as byte arrays and interpret them later on.
See also: Creating a .mat file of v7.3 in python, Strings in HDF5.

Import MTX files into R

I have a large hdf file from which I get one of the data-frame and convert it into Sparse Matrix in python (sparse.csr_matrix). Now, I save this as .MTX file and trying to load this in R. I went through some documentation and links for loading MTX files into R using externalFormats {Matrix}. Unfortunately, I get the following error.
TestDataMatrix = readMM(system.file("./Downloads/TestDataMatrix.mtx",
package = "Matrix"))
When I run the above code, I get the following error and I have no clue what it means.
TestDataMatrix = readMM(system.file("./Downloads/TestDataMatrix.mtx",
+ package = "Matrix"))
1:
Could someone let me know if there is an easy way to convert python objects to R objects (like RDS).
I found a way to read hdf files in R and got the data-frame converted to Sparse-Matrix. But there is a problem writing to HDF files. I posted a new question here.

Pass data between MATLAB R2011b and Python (Windows 7)

Hello Friends,
I want to pass data between MATLAB and Python, One way would be to use matlab.engine in Python or Call Python Libraries from MATLAB. But this approach requires MATLAB 2014 Version unlike mine which is MATLAB R2011b.
So I request you to please guide for a different Approach in order to comunicate between Python and MATLAB R2011b Version.
Thanks in advance
Both matlab and python support hdf5 binary file format.
You can read/write hdf5 data files in matlab using hdf5read/hdf5write:
>> hdf5write('./data_from_matlab.h5', '/data', x);
In python you have h5py:
import h5py, numpy as np
with h5py.File('./data_from_matlab.h5', 'r') as R:
x = np.array(R['data'])
The other way around:
import h5py, numpy as np
with h5py.File('./data_from_python.h5', 'w') as W:
W.create_dataset(name='data', data=np.zeros((10,10),dtype='f4'))
And reading in Matlab
>> data = hdf5read('./data_from_python.h5','/data'); % you might need to remove '/' from '/data'...
Depending on what you want to do and your type of data, you could write it to a file and read from it in the other language. You could use numpy.fromfile for that in the python part.

Creating a .mat file of v7.3 in python

I need to perform multiplication involving 60000X70000 matrix either in python or matlab. I have a 16GB RAM and am able to load each row of the matrix easily (which is what I require). I am able to create the matrix as a whole in python but not in matlab.
Is there anyway I can save the array as .mat file of v7.3 using h5py or scipy so that I can load each row separately?
For MATLAB v7.3 you can use hdf5storage which requires h5py, download the file here, extract, then type: python setup.py install from a command prompt.
https://pypi.python.org/pypi/hdf5storage
import h5py
import hdf5storage
import numpy as np
matfiledata = {} # make a dictionary to store the MAT data in
matfiledata[u'variable1'] = np.zeros(100) # *** u prefix for variable name = unicode format, no issues thru Python 3.5; advise keeping u prefix indicator format based on feedback despite docs ***
matfiledata[u'variable2'] = np.ones(300)
hdf5storage.write(matfiledata, '.', 'example.mat', matlab_compatible=True)
If MATLAB can't load the whole thing at once, I think you'll have to save it in different variables matfiledata[u'chunk1'] matfiledata[u'chunk2'] matfiledata[u'chunk3'] etc.
Then in MATLAB if you save each chunk as a variable
load(filename,'chunk1')
do stuff...
clear chunk1
load(filename,'chunk2')
do stuff...
clear chunk2
etc.
The hdf5storage.savemat has a parameter to allow the file to be read into Python correctly in the future so worth checking out, and follows the scipy.io.loadmat format... although you can do something like this if saving data from MATLAB to make it easy to import back into Python:
MATLAB
save('example.mat','-v7.3')
Python
matdata = hdf5storage.loadmat('example.mat')
That will load back into Python as a dictionary which you can then convert into whatever datatypes you need.

Categories