Change dtype data_type fields of numpy array
<p>I have a <code>.mat</code> file which I load using <code>scipy</code>:</p>

<pre><code>from oct2py import octave
import scipy.io as spio
matPath = "path/to/file.mat"
matFile = spio.loadmat(matPath)
</code></pre>

<p>I get a numpy array which I want to pass to octave function using <code>oct2py</code>, like that:</p>

<pre><code>aMatStruct = matFile["aMatStruct"]
result = octave.aMatFunction(aMatStruct)
</code></pre>

<p>But I get the following error:</p>

<pre><code>oct2py.utils.Oct2PyError: Datatype not supported
</code></pre>

<p>It seems it's due to the array <code>dtype</code>, which looks like: <code>[('x', 'O'), ('y', 'O'), ('z', 'O')]</code></p>

<p>So I thought about changing it to <code>'S'</code>, <code>'U'</code>, or something that's supported.</p>

<ol>
<li>How can it be done?</li>
<li>I couldn't find a supported dtype in the <a href="https://blink1073.github.io/oct2py/source/examples.html" rel="nofollow">otc2py documentation</a>. Any help on that?</li>
<li>Is there a way to load the <code>.mat</code> file with another (supported) dtype? I looked <a href="http://docs.scipy.org/doc/scipy/reference/generated/scipy.io.loadmat.html#scipy.io.loadmat" rel="nofollow">here</a> but couldn't find anything useful.</li>
</ol>

<p>Note that inside <code>aMatFunction</code> it uses <code>aMatStruct</code> like that: <code>x = aMatStruct.x</code>.</p>

<p>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</p>

<p>However, the other, simpler solution I was going to suggest is that, since you're only using the struct within octave, you <em>could</em> just load it into its workspace and evaluate directly. i.e. the following works for me:</p>

<pre><code>&gt;&gt;&gt; octave.load("MyFile.mat")
&gt;&gt;&gt; octave.eval("fieldnames(S)")
ans = 
{
  [1,1] = name
  [2,1] = age
}
[u'name', u'age']
</code></pre>

<p><strong>EDIT</strong> eh screw it, here's the python solution; I'd started it anyway :p</p>

<pre><code>&gt;&gt;&gt; from oct2py import octave
&gt;&gt;&gt; from scipy.io import loadmat
&gt;&gt;&gt; F = loadmat("/home/tasos/Desktop/MyFile.mat")
&gt;&gt;&gt; M = F["S"] # the name of the struct variable I saved in the mat file
&gt;&gt;&gt; M = M[0][0]
&gt;&gt;&gt; D = {M.dtype.names[i]:M[i][0] for i in range(len(M))}
&gt;&gt;&gt; out = octave.fieldnames(D); out
[u'age', u'name']
</code></pre>

python numpy scipy octave