1

I have a .mat dataset obtained from Matlab that have some data, including an 1920x1 Cell Array String stored as follow:

"sample1"
"sample2"
"sample3"

I need to import it in Python as list or array. I already got my mat using loadmat from scipy and works fine importing other type of data, but I'm facing issues trying to import cell string array:

import scipy.io as spio
import pandas as pd
import numpy as np

mat = spio.loadmat('Dati.mat', squeeze_me=True)
FD_Labels_cell = mat_char['FD_Labels']
list= np.asarray(FD_Labels_cell, dtype=object).tolist()
print(list)

output:

[[MatlabOpaque([(b'', b'MCOS', b'string', array([[3707764736],
       [         2],
       [         1],
       [         1],
       [         1],
       [         1]], dtype=uint32))],
             dtype=[('s0', 'O'), ('s1', 'O'), ('s2', 'O'), ('arr', 'O')])], [MatlabOpaque([(b'', b'MCOS', b'string', array([[3707764736],
       [         2],
       [         1],
       [         1],
       [         2],
       [         1]], dtype=uint32))],
             dtype=[('s0', 'O'), ('s1', 'O'), ('s2', 'O'), ('arr', 'O')])], [MatlabOpaque([(b'', b'MCOS', b'string', array([[3707764736],
       [         2],
       [         1],
       [         1],
       [         3],
       [         1]], dtype=uint32))],
             dtype=[('s0', 'O'), ('s1', 'O'), ('s2', 'O'), ('arr', 'O')])], [MatlabOpaque([(b'', b'MCOS', b'string', array([[3707764736]

and output goes on. I don't know why I get this kind of output and I'm unable to get cells from my Matlab file.

3
  • 1
    MatlabOpaque means the cell contains MATLAB objects that loadmat can't convert to python equivalents. Commented Jan 17, 2021 at 14:33
  • So it's incompatible. Well, thanks! Commented Jan 17, 2021 at 17:47
  • Could you convert your cell array to a table, then write it to a csv (writetable)? Commented Jan 18, 2021 at 13:46

2 Answers 2

0

That is not a cell array, it is a string. It is a different data type. Matlab has 3 different data types for text:

  1. char array. Looks like 'this is a char' or [a b';'c d']`. Note the single quotes and square brackets). This is sometimes called a string in the docs for historical reasons.
  2. cell string, which is literally just a cell array of char arrays. Looks like {'a b'}. Note the single quotes and curly brackets. This goes by several names, cell array of strings, cell string, or cellstr.
  3. string array. Looks like "a b" or ["a b", "c"]. Note the double quotes. This is completely different data type than the first two, and is also much, much newer so it is apparently not supported by scipy.

If you converted it to a cell array with the cellstr function in MATLAB before saving it you should be able to load it correctly.

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

Comments

0

You are exporting the mat file with variables in a wrong format. Convert the format to cellstr in matlab, export the file and then read it using scipy. Then you can apply df functions on it by creating a dataframe. I converted it to cellstr, export again from matlab and then read using scipy and it worked. reference can be found on my git repo: https://github.com/fahnub/matlab-datetime-data-extraction

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

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.