0

I'm trying to import a .mat file that contains a cell array. In matlab I did this:

a={'element1';'element2';'element3'};
save('m.mat','-v7','a')

I used '-v7' because, in python, I use 'scipy.io' and 'loadmat' that can be used only with version 7. In python:

import scipy.io as sio
i=sio.loadmat('m.mat')
i.keys()

And I get:

dict_keys(['__header__', '__version__', '__globals__', 'a'])

Finally, I use the key 'a':

k=i['a']

In the variable explorer I get k, that is a object type but I can't access to its elements. How can I convert it to a list?

variable explorer

Thank you

1 Answer 1

1

I noticed that numpy.savetxt can convert your data to csv with one string column. So, I tried to follow what it does. And I think this is what you want:

val = []
for row in k:
    v = '%s' % tuple(row)
    val.append(v)

If your data is numeric such as float, you can try this:

val = np.asarray(k, dtype='float').tolist()
Sign up to request clarification or add additional context in comments.

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.