2

I have a .mat file with variables containing numbers and strings. When I load it and get the variable containing strings I don't understand how to actually get the string out of it:

data = scipy.io.loadmat(pathName)
featurenames=data['featurenames']
print(featurenames[0:2,0])

As an output I get:

[array(['Intensity_SubsBlue_Nuclei_1_IntegratedIntensity'], dtype='<U47')
 array(['Intensity_SubsBlue_Nuclei_2_MeanIntensity'], dtype='<U41')]

How do I get to this array? I want to have just strings.

Thank you!

3 Answers 3

1

I am not very familiar reading Matlab files, but you already got an array. If you want to put each string in a variable what you can do is:

string1 = featurenames[0][0]
string2 = featurenames[1][0]

Please, if this is not the answer you are looking, could you be more specific on your question? thanks!

Sign up to request clarification or add additional context in comments.

Comments

0

It looks like featurenames is a cell in MATLAB. All matrices and cells are 2d in that language. Cells can contain a mix of elements, size and type. That's more like a python list than a numpy numeric array. loadmat returns that as a 2d object dtype array - containing arrays.

You selected featurenames[0:2, 0], which returns 2 of those cell elements as a 1d array.

I can recreate your array with:

In [9]: arr = np.empty(2, dtype=object)
In [11]: arr[:] = [np.array(['Intensity_SubsBlue_Nuclei_1_IntegratedIntensity'],
    ...:  dtype='<U47'),
    ...:  np.array(['Intensity_SubsBlue_Nuclei_2_MeanIntensity'], dtype='<U41')]
    ...: 
    ...:  
In [12]: arr
Out[12]: 
array([array(['Intensity_SubsBlue_Nuclei_1_IntegratedIntensity'], dtype='<U47'),
       array(['Intensity_SubsBlue_Nuclei_2_MeanIntensity'], dtype='<U41')],
      dtype=object)
In [13]: print(arr)
[array(['Intensity_SubsBlue_Nuclei_1_IntegratedIntensity'], dtype='<U47')
 array(['Intensity_SubsBlue_Nuclei_2_MeanIntensity'], dtype='<U41')]

So you have to access the elements, and then the element within each of those:

In [14]: arr[0][0]
Out[14]: 'Intensity_SubsBlue_Nuclei_1_IntegratedIntensity'
In [15]: [a.item() for a in arr]
Out[15]: 
['Intensity_SubsBlue_Nuclei_1_IntegratedIntensity',
 'Intensity_SubsBlue_Nuclei_2_MeanIntensity']

For a single element array, [0] or item() work equally well.

Or the outer elements can be joined into one array with concatenate. Note the change in dtype:

In [16]: np.concatenate(arr)
Out[16]: 
array(['Intensity_SubsBlue_Nuclei_1_IntegratedIntensity',
       'Intensity_SubsBlue_Nuclei_2_MeanIntensity'], dtype='<U47')
In [17]: _[0]
Out[17]: 'Intensity_SubsBlue_Nuclei_1_IntegratedIntensity'

Comments

0

What you appear to have is a list of scipy/numpy arrays. The problem you're experiencing is that while datatype doesn't strictly matter to Python, it definitely does to scipy and numpy, and I assume you're looking to get these to Python strings. The snippet below should do it (assuming your output is named matFileOut):

## for the zeroth array in your list, convert the scipy/numpy array to a Python list, then take the zeroth (only) element
matFileOut[0].tolist()[0]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.