0

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

Shivkumar kondi
  • 6,458
  • 9
  • 31
  • 58
Abhishek
  • 515
  • 1
  • 5
  • 12

2 Answers2

1

Both and support 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'...
Shai
  • 111,146
  • 38
  • 238
  • 371
  • Thank Shai for quick Response. I tried but I was just able to create .h5 file in MATLAB but when tried to read in Ipython, I wasnt able to...Error was OSERROR: Unable to open the file – Abhishek Jan 25 '17 at 12:44
  • @Abhishek does the file exist? do you have read permissions to the file created? is it possible the file is corrupted? if you type in shell `h5ls ` what do you see? – Shai Jan 25 '17 at 12:50
  • Yes, I created in MATLAB and when I entered in cmd...It gave the path and data with { 10, 10} – Abhishek Jan 25 '17 at 14:52
  • Now I cross checked, file is present in the Location but unable to open in python – Abhishek Jan 25 '17 at 15:06
  • with h5py.File(''(Complete link with '/' separation)./data_from_matlab.h5', 'r') as R: x = np.array(R['(Complete link with '/' separation) data']) but got KeyError: 'Unable to open object(Component not found)' – Abhishek Jan 25 '17 at 15:28
  • Sorry Shai.....I entered ls -ltrh in Ipython...Volume in drive C is SYSTEM Volume Serial Number is 64E0-8641 Directory of (Link) – Abhishek Jan 25 '17 at 15:30
  • @Abhishek what is `"''(Complete link with '/' separation)./data_from_matlab.h5',`? – Shai Jan 25 '17 at 15:32
  • Complete link means Link to the Folder....Okay This is what I mean G:/computer/Abhishek_HonnavalliPuttaiah/Input_Signals/Processing – Abhishek Jan 25 '17 at 15:35
  • @Abhishek you need "complete link/path" only for the file name, when you access the variable in `R`, you only need the variable name: `R['data']` and not `R[full/path/data]` - this is the reason for `KeyError` you get. – Shai Jan 25 '17 at 15:40
  • @Shai....I edited and tried on Ipython but still gave Key Error . Unable to open object (Object 'mean.xlsx' doesn't exist) – Abhishek Jan 25 '17 at 16:26
  • @Abhishek what R.keys() gives you? – Shai Jan 25 '17 at 16:28
  • AttributeError : 'File ' object has no Attribute 'key' – Abhishek Jan 25 '17 at 16:31
  • @Abhishek what about R['/data']? – Shai Jan 25 '17 at 16:32
  • In my case data is mean.xlsx . So I gave R['/mean.xlsx' ] but Value Error: Not a location – Abhishek Jan 25 '17 at 16:34
0

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.

UpSampler
  • 399
  • 2
  • 6
  • Thanks UpSampler for creative Approach. Actually my actually Format of the data file is .dat . So how to read such a file, I tried xls way of importing but didnt work becasue file size was 350MB – Abhishek Jan 25 '17 at 12:45