Change Dtype Data_type Fields Of Numpy Array
I have a .mat file which I load using scipy: from oct2py import octave import scipy.io as spio matPath = 'path/to/file.mat' matFile = spio.loadmat(matPath) I get a numpy array whi
Solution 1:
Found a workaround!
I used the following script, which reconstructs python dictionaries from the loaded struct, which oct2py2
in turn interpret as matlab structs.
Solution 2:
I was going to suggest converting the resulting array to a valid dictionary but you beat me to it so I won't code it :p
However, the other, simpler solution I was going to suggest is that, since you're only using the struct within octave, you could just load it into its workspace and evaluate directly. i.e. the following works for me:
>>> octave.load("MyFile.mat")
>>> octave.eval("fieldnames(S)")
ans =
{
[1,1] = name
[2,1] = age
}
[u'name', u'age']
EDIT eh screw it, here's the python solution; I'd started it anyway :p
>>> from oct2py import octave
>>> from scipy.io import loadmat
>>> F = loadmat("/home/tasos/Desktop/MyFile.mat")
>>> M = F["S"] # the name of the struct variable I saved in the mat file>>> M = M[0][0]
>>> D = {M.dtype.names[i]:M[i][0] for i inrange(len(M))}
>>> out = octave.fieldnames(D); out
[u'age', u'name']
Post a Comment for "Change Dtype Data_type Fields Of Numpy Array"