I am saving a structured numpy array of the to a mat file using scipy.io.savemat this way:
sio.savemat(filename, { ‘myStructuredArray’: myStructuredArray}, appendmat=True )
then I reload it this way:
mat = sio.loadmat( filename, squeeze_me=True) # mat actually contains additional variables not shown here
myStucturedArray = mat['myStucturedArray']
The content of the reloaded array is correct but the dtype of the reloaded array has changed.
Before saving dtype looks like this:
[('time', '<f8'), ('eType', '<i8'), ('name', 'S10')]
but after reload it looks like this instead:
[('time', 'O'), ('eType', 'O'), ('name', 'O')]
therefore when I subsequently try to append further structured data ( of form [('time', '<f8'), ('eType', '<i8'), ('name', 'S10')]) to that reloaded array python throws the following error:
TypeError: invalid type promotion with structured datatype(s).
How can i make sure that dtypes are preserved when I use savemat?
.mat, and know that MATLAB struct are returned as structured arrays. And that the use ofobjectdtype is common. But I haven't tried this kind of write and read. I wonder what this mat looks like when loaded in MATLAB/Octave. Keep in mind that this pair of functions is intended primarily for MATLAB compatibility, not as a transparentnumpystorage. Thenp.save/loadpair are better for purenumpyuse.loadmatequivalent of a MATLAB cell array is an object dtype numpy array. mathworks.com/help/matlab/matlab_prog/…