Without looking at your errors in detail I can point out some pitfalls.
The .mat will contain MATLAB matrices (always 2d or higher), cells and structures.
loadmat
renders those in various ways. There are dictionaries that you have to index by name. There are object arrays (dtype=object). And there are nd numeric or string arrays. You may have to work through several levels to get at the numeric array.
Check the 'shape' (size) of an array and its 'dtype'. If shape is ()
and dtype
object, then extract it with y=x[()]
.
Here's an example of such a 0d object array:
In [4]: y=np.arange(3)
In [5]: x=np.empty((), dtype=object)
In [6]: x[()]=y
In [7]: x
Out[7]: array(array([0, 1, 2]), dtype=object)
In [8]: x.shape
Out[8]: ()
In [9]: x.dtype
Out[9]: dtype('O')
In [10]: x[0]
...
IndexError: too many indices for array
In [11]: x[()]
Out[11]: array([0, 1, 2])
x
is a 0d array (x.ndim), so it must be indexed with a 0 element tuple, ()
. For a MATLAB programmer that can seem odd.
In numpy
(Python in general), x[a,b,c]
is the same as x[(a,b,c)]
and ind=(a,b,c); x[ind]
. In other words, the arguments in []
are understood to be a tuple of values. (1,2)
is a 2 element tuple, (1,)
is one element ( (1)
is just a grouping), and ()
is a 0 element tuple. So x[()]
is just an extension of the regular nd
indexing notation. It isn't a special case.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…