user74326
user74326

Reputation: 23

Loading a Cell Array (.mat file) into Python

I have a 25x4x3 Cell Array which I have saved into a .mat file and now need to load into Python.

A simple .mat file I am familiar with, but being new to Python, this problem is a little over my head. The 25 dimensions and 4 dimensions represent integers that I need to call later in my code. The 3 dimension in the cell array contains a list of datetime arrays, data, and then more character arrays of labels for that data.

e.g. (pseudocode)

.mat(1,1,1) = datetime arrays; 
.mat(1,1,2) = data corresponding to datetime arrays; 
.mat(1,1,3) = labels for each column in that data. 

Obviously I need to load this into Python with loadmat('.mat') and then I get confused because my variable explorer does not show the .mat file. I have no idea how to pick apart the contents to correctly convert datettime indices accordingly, numbers to floats, etc.

Any guidance would be appreciated!

Right now I simply have:

filemat = 'Pdata.mat'
Pdata = sio.loadmat(filemat)

It is loading the .mat as an object from what I can tell.

Upvotes: 2

Views: 5549

Answers (2)

Iqbal Jurist
Iqbal Jurist

Reputation: 43

How about assigning a variable that only contains a ranged value of 'a'? is it possible like this?

x1 = mat['a', range(1, 20, 1)]

Upvotes: 0

Poelie
Poelie

Reputation: 713

You're almost there.

First let me build some sample matlab data:

a = num2cell(rand(25,4,3));
a{13} = 'abc';
save('/tmp/tmp4Python.mat','a')

Now load it in Python

import scipy.io
mat = scipy.io.loadmat('/tmp/tmp4Python.mat')

And assign the variable

a = mat['a']

Now if we want to access the 'abc' string we do it as follows (note that Python indexing starts at 0, whereas MATLAB indexing starts at 1):

a[12][0][0]

Upvotes: 2

Related Questions