Search code examples
matlabcell-array

Convert cells inside a cell array to cell arrays - matlab


Do not get confused with row numbers in the arrays. I am just asking converting 1 into style 2

This is Cell Array C

cell array 1

I want to convert it to the below version how can i do that ?

V2 =

cell array 1 converted to v2 enter image description here


Solution

  • If I understand correctly, you have a flat cell array of strings (which contain valid MATLAB cell array definitions), and you want to convert each string into a sub-cell-array of its own. So what you need is:

    cellfun(@eval, C, 'UniformOutput', false)
    

    Where C is the original cell array of strings.

    Example:

    C = {'{''samsung'', ''n150'', ''jp0xtr'', ''n570''};'; ...
        '{''samsung'', ''n150'', ''jp0xtr'', ''beyaz''};'}
    C2 = cellfun(@eval, C, 'UniformOutput', false)
    

    The result is:

    C = 
    
        '{'samsung', 'n150', 'jp0xtr', 'n570'};'
        '{'samsung', 'n150', 'jp0xtr', 'beyaz'};'
    
    
    C2 = 
    
        {1x4 cell}
        {1x4 cell}