Azzera filtri
Azzera filtri

Error using vertcat Dimensions of arrays being concatenated are not consistent.

12 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Chunru
Chunru il 15 Lug 2022
Modificato: Chunru il 15 Lug 2022
% some_var = ['AB'; 'CD', 'EF']
% semicolon for new rows and comma for new coloumn. so the above is wrong
% The code below is OK (each row has same number of characters in order to
% form a 2d char array
some_var = ['AB'; 'CD'; 'EF']
some_var = 3×2 char array
'AB' 'CD' 'EF'
% some_var_full = ['alpha beta'; 'gamma eps'; 'delta phi']
% The above is wrong since the 1st line has more characters than other
% lines
% You can make each line has same number of characters
some_var_full = ['alpha beta'; 'gamma eps '; 'delta phi ']
some_var_full = 3×10 char array
'alpha beta' 'gamma eps ' 'delta phi '
% It is better to use string array
some_var_full = ["alpha beta"; "gamma eps"; "delta phi"]
some_var_full = 3×1 string array
"alpha beta" "gamma eps" "delta phi"

Più risposte (1)

Walter Roberson
Walter Roberson il 15 Lug 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.

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Tag

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by