Search code examples
stringmatlabcell-array

Parsing string of a cell to cell


I have the following string in MATLAB:

contents = '{'aa' 'bb' 'cc' 'dd'}'

I need to get the 1x4 cell-array inside it, like:

mycell = {'aa' 'bb' 'cc' 'dd'}

How would you do it? Is there a quicker way than parsing it?


Solution

  • Don't forget the double quote inside the string and use eval (ugly solution but simple and working).

    >> contents = '{''aa'' ''bb'' ''cc'' ''dd''}'
    
    contents =
    
    {'aa' 'bb' 'cc' 'dd'}
    
    >> c = eval(contents)
    
    c = 
    
        'aa'    'bb'    'cc'    'dd'
    
    >> class(c)
    
    ans =
    
    cell
    
    >> c{2}
    
    ans =
    
    bb