Error using vertcat Dimensions of arrays being concatenated are not consistent.
Show older comments
This seems to be a trivial problem but I am struggling to figure it out. Why is it that when I define
some_var = ['AB'; 'CD'; 'EF'],
then there is no error, but when I write
some_var_full = ['alpha beta'; 'gamma eps'; delta phi'], it generates an error mesage:
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
How can I fix it? Thank you so much in advance for helping me.
Accepted Answer
More Answers (1)
Walter Roberson
on 15 Jul 2022
'AB' is a 1 x 2 array of character
'CD' is a 1 x 2 array of character
['AB';'CD'] is asking to vertically concatenate a 1x2 and a 1x2. The number of columns match so the result is well defined to create 2x2 array of character
'alpha beta' is a 1x10 array of character
'gamma eps' is a 1x9 array of character.
When you [;] those together you are asking to vertically concatenate a 1x10 array and a 1x9 array. The number of columns do not match so it is an error.
You might want to switch to using
some_var_full = ["alpha beta"; "gamma eps"; "delta phi"]
Now each of the rows is a 1x1 string() object instead of being character vectors.
1 Comment
alphabetagamma
on 15 Jul 2022
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!