简体   繁体   中英

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.

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM