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?
loading:
So the returned array is structured with object dtype fields, and each entry is a
numpyarray (2d) with dtypes matching the original.The Octave load looks like a normal
struct. The class isn't evident in the display, but does show as expected in the Workspace directory window.In Octave
As I commented, the primary goal of the
savemat/loadmatpair is to interact withMATLABseamlessly (as possible). And it seems to be to doing just that. Usenp.save/loadfor transparentnumpyround trips.