How to access elements of numpy ndarray?
<p>I'm using scipy's <code>loadmat</code> function to load a matlab data file into python.</p>

<pre><code>from scipy.io import loadmat  

data   = loadmat('data.mat')
fields = data['field']
</code></pre>

<p>The type of <code>fields</code> is <code>numpy.ndarray</code>:</p>

<pre><code>print 'fields type={}'.format(type(fields))
print 'fields dtype={}'.format(fields.dtype)
print 'fields shape={}'.format(fields.shape)
</code></pre>

<blockquote>
<pre><code>fields type=&lt;type 'numpy.ndarray'&gt;
fields dtype=object
fields shape=(5,)
</code></pre>
</blockquote>

<p>I iterate over the array using <code>nditer</code>: </p>

<pre><code>for x in np.nditer(fields, flags=['refs_ok']):
    print 'x={}'.format(x)
    print 'x type={}'.format(type(x))
    print 'x dtype={}'.format(x.dtype)
    print 'x shape={}'.format(x.shape)
    break
</code></pre>

<blockquote>
<pre><code>x=[u'ACE']
x type=&lt;type 'numpy.ndarray'&gt;
x dtype=object
x shape=()
</code></pre>
</blockquote>

<p><strong>IndexError:</strong></p>

<p>If I try to access the first element of <code>x</code> I get an <code>IndexError</code>:</p>

<pre><code>x[0]
</code></pre>

<blockquote>
<pre><code>---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
&lt;ipython-input-102-8c374ae22096&gt; in &lt;module&gt;()
     17     print 'type={}'.format(type(x))
     18     print 'dtype={}'.format(x.dtype)
---&gt; 19     x[0]
     20     break
     21 

IndexError: too many indices for array
</code></pre>
</blockquote>

<p><strong>Questions:</strong></p>

<ul>
<li>How come, if <code>type(x)</code> returns <code>nump.ndarray</code> it says "too many indices for array"?</li>
<li>How can I extract the contents of <code>x</code> into a string?</li>
</ul>

<p><strong>Here are the versions I'm using:</strong></p>

<pre><code>print 'python version: {}'.format(sys.version)
print 'numpy version: {}'.format(numpy.__version__)
print 'scipy version: {}'.format(scipy.__version__)
</code></pre>

<blockquote>
<pre><code>python version: 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2]
numpy version: 1.11.0
scipy version: 0.17.1
</code></pre>
</blockquote>