Search code examples
matlabcell-array

Matlab error with vertcat and cell arrays


I'm trying to concatenate two arrays as follows:

z={ '35' {'test'} ; '45' {'test'}}
z={z{:} ;{'55' {'test'}}}

I would expect the result to be

 {35 {'test'}
 45  {'test'}
 55 {'test'}}

but instead I get:

Error using vertcat

Dimensions of matrices being concatenated are not consistent.

What am I forgetting? Thanks.


Solution

  • The error is caused by z{:} which lists all content of z 'into' a N by 1 vector and when you try to collect all the elements with the outer {} it throws the error due to mismatching dimensions.

    You might be using too many { } and you can concatenate cell arrays with [ ]:

    z = { '35' 'test'
         '45'  'test'};
    z = [z; {'55' 'test'}]
    

    The command window will display:

    z = 
        '35'    'test'
        '45'    'test'
        '55'    'test'