Search code examples
arraysmatlabnestedcellcell-array

How to separate data from nested cells?


I have a nested cell as given below

A= {1x12 cell}  {1x12 cell}  {1x12 cell}  {1x12 cell}  {1x12 cell}

I had tried A{:} for getting the data in the above cells and I obtain it as below

ans = 

 Columns 1 through 12

'1'    '0'    '1'    '0'    '1'    '0'    '0'    '1'    '1'    '1'    '1'    '1'



 ans = 

 Columns 1 through 12

'1'    '1'    '0'    '1'    '1'    '1'    '1'    '0'    '1'    '1'    '0'    '0'




 ans = 

 Columns 1 through 12

'0'    '1'    '1'    '1'    '0'    '0'    '0'    '0'    '1'    '1'    '0'    '0'




 ans = 

  Columns 1 through 12

'1'    '1'    '1'    '1'    '0'    '1'    '1'    '0'    '0'    '0'    '0'    '1'




  ans = 

  Columns 1 through 12

'0'    '0'    '1'    '0'    '0'    '1'    '0'    '1'    '0'    '0'    '0'    '1'

I want to have the binary data inside each cell in separate vectors stored in variables. My desired output is as follows,

  a1=[1    0    1    0    1    0    0    1    1    1    1    1    ]

  a2=[1    1    0    1    1    1    1    0    1    1    0    0    ]

  a3=[0    1    1    1    0    0    0    0    1    1    0    0    ]

  a4=[1    1    1    1    0    1    1    0    0    0    0    1    ]

  a5=[0    0    1    0    0    1    0    1    0    0    0    1    ]

How to achieve such a result? Thanks in advance.


Solution

  • You'd better use a matrix (as suggested by Divakar):

    M = reshape(cell2mat([A{:}]),[],numel(A)).';
    

    Or more simply, as noted by knedlsepp:

    M = cell2mat(cat(1,A{:}));
    

    Then your desired "variables" are the rows of M, that is, M(1,:), M(2,:) etc.