2

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.

2
  • Pdata should be a dictionary, Pdata['mat'] should be the object encoding your mat cell. Because it comes from a cell with mixed data, it won't be a simple numpy numeric array. Previous loadmat questions have demonstrated how to extract information from such structures (see the side bar). You may have to show us the variable (if it's not too big). Commented Jul 21, 2017 at 16:35
  • My last answer along this line: stackoverflow.com/a/42258001/901925; or stackoverflow.com/questions/32723802/… Commented Jul 21, 2017 at 16:39

2 Answers 2

2

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]
Sign up to request clarification or add additional context in comments.

2 Comments

how do you deal with it if it is a file save using format v7.3?
@Tulkkas In that case, you can import hdf5storage then hdf5storage.loadmat(file.mat)
0

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)]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.