Without looking again at your previous question I suspect the issue is with now numpy.array
creates arrays from lists of sublists or arrays.
You note that markerList
is saved as expected, and that the cells vary in size.
Try
np.array(markerList)
and look at its shape and dtype. I'm guessing it will be 1d (200,), and object dtype.
np.array(finalStack)
on the other hand probably will be the 3d array it saves.
savemat
is set up to save numpy arrays, not python dictionaries and lists - it is, after, all talking to MATLAB where everything used to be a 2d matrix. MATLAB cells generalize this; they are more like 2d numpy arrays of dtype object.
The issue of creating an object array from elements that uniform in size comes up often. The usual solution is to create empty
array of the desired size (e.g. (200,)) and object type, and load the subarrays into that.
https://stackoverflow.com/a/38776674/901925
=============
I'll demonstrate. Make 3 arrays, 2 of one size, and different third:
In [59]: from scipy import io
In [60]: A=np.ones((40,2))
In [61]: B=np.ones((40,2))
In [62]: C=np.ones((30,2))
Save two lists, one with just two arrays, the other with all three:
In [63]: io.savemat('test.mat', {'AB':[A,B],'ABC':[A,B,C]})
Load it back; I could do this in octave
instead:
In [65]: D=io.loadmat('test.mat')
In [66]: D.keys()
Out[66]: dict_keys(['ABC', '__header__', 'AB', '__globals__', '__version__'])
ABC
is a 2d array with 3 elements
In [68]: D['ABC'].shape
Out[68]: (1, 3)
In [71]: D['ABC'][0,0].shape
Out[71]: (40, 2)
but AB
has been transformed into a 3d array:
In [69]: D['AB'].shape
Out[69]: (2, 40, 2)
In [70]: np.array([A,B]).shape
Out[70]: (2, 40, 2)
If I instead make a 1d object array to hold A and B, it is preserved:
In [72]: AB=np.empty((2,),object)
In [73]: AB[...]=[A,B]
In [74]: AB.shape
Out[74]: (2,)
In [75]: io.savemat('test.mat', {'AB':AB,'ABC':[A,B,C]})
In [76]: D=io.loadmat('test.mat')
In [77]: D['AB'].shape
Out[77]: (1, 2)
In [78]: D['AB'][0,0].shape
Out[78]: (40, 2)
A good alternative is to save the arrays as items of a dictionary
io.savemat('test.mat',{'A':A, 'B':B, 'C':C})
Given the difficulties in translating MATLAB structures to numpy ones and back, it's better to keep things flat and simple, rather than create compound objects that would be useful on both sides.
===============
I installed Octave
. Loading this test.mat
:
io.savemat('test.mat', {'AB':AB,'ABs':[A,B]})
gives
>> whos
Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
AB 1x2 1280 cell
ABs 2x40x2 1280 double
An object dtype array is saved as a matlab cell; other arrays as matlab matrices. (I'd have to review earlier answers to recall the equivalent of matlab structures).