2

I've read a .mat file with scipy.io:

data = scipy.io.loadmat(etc.)

The function returns a dictionary, and my Matlab structure array is stored in a Numpy structured array. So far, so good. One of my fields is called repet1_F3, and should contain a vector of floats. I've accessed the vector using:

repet1_F3= data['repet1_F3']

repet1_F3 has a weird structure that I can't manipulate:

>>> repet1_F3
array(array([ 0.48856978,  0.74278461, 2.73300925,  2.72642893, 2.73684854,  2.74516561,  2.69143553]), dtype=object)

Am I doing something wrong? How could I convert this object into a standard numpy array?

2
  • What does this data look like in matlab? Commented Apr 13, 2016 at 1:28
  • And what is data.shape? Commented Apr 13, 2016 at 1:29

1 Answer 1

1

loadmat tends to wrap MATLAB structures in numpy object arrays.

array(array([ 0.48856978,  0.74278461, 
 2.73300925,  2.72642893,  2.73684854,  2.74516561,  2.69143553]), dtype=object)

Looks like a 1 element array of dtype object; that element is itself a 1d float array. The outer array probably has shape () (0d).

Try repet1_F3.item() or repet1_F3([]). One of those should give the inner array.

Do you know the MATLAB structure that contains numbers like this?

correction - it should be repet1_F3[()].

Sign up to request clarification or add additional context in comments.

4 Comments

repet1_F3.item() worked, thank you. The Matlab structure is a structure array. Numbers displayed in Matlab have a 4 decimals precision, but they have more in python. Don't know why.
The 4 decimals is just a display setting; the actual values are 32 or 64 bit floats.
I think repet1_F3([]) should be repet1_F3[()]
you are right - the indexing [] go outside, the empty tuple inside. That's what happens when I don't have a live shell to test things in.

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.