MATLAB Answers

Why do I get the error 'CAT arguments dimensions are not consistent'?

116 views (last 30 days)
Why do I get the following error message:
Error: CAT arguments dimensions are not consistent.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Feb 2012
You are attempting to concatenate two arrays using [] but the appropriate dimensions along which to concatenate are not the same size.
Common causes:
You have forgotten a semicolon (;) or a comma (,) when attempting to concatenate two character arrays, or you are using the wrong separator. This error commonly occurs when attempting to concatenate character arrays to try to create a multi-line string.
Examine the line listed in the error message and insert the appropriate matrix element separator. Also verify that the dimensions along which you are trying to concatenate the matrices agree. The help files for the HORZCAT and VERTCAT functions describe the relationship that needs to exist for this concatenation to work correctly.
is the equivalent of [A B] while
is the equivalent of [A;B]. If you are attempting to concatenate strings, you should use the STRCAT and STRVCAT functions, which handle adjusting the appropriate dimensions automatically if it is necessary.
Example demonstrating this error:

More Answers (2)

Hirak on 19 Dec 2013
I have also experienced the same when I run the following code:
clear all;
close all;
for r_core=(1:1:17);
xlabel('Shell thickness(nm)')
ylabel('Particle radius(nm)');
zlabel('Murray g factor');
data = [r_shell;r_core;g];
Please reply.Hirak

Fox on 18 Jan 2014
Edited: Fox on 18 Jan 2014
I have run into the same error when trying to do this within a while loop initialized as counter = 2
In this case I had the following variables: Data = a {counter+1 x 1} cell array where each row was a {1 x 6} cell array
with each run of the loop, the program's gui was asking the user for a few inputs which could have a pretty broad range
I wanted to get the first, second, and third columns as inputs for a continuously iterating plot that would be shown as the inputs were being entered so I tried this
u = vertcat(Data{2:counter+1});
u_date = str2num(cell2mat(u(1:counter,1)));
u_cr = str2num(cell2mat(u(1:counter,2)));
u_hg = str2num(cell2mat(u(1:counter,3)));
it was going swimmingly until I tried to enter for the 3rd column's value "8" and then "10". At which point I got the error you describe. I changed my code to be this:
u = vertcat(Data{2:counter+1});
u_date = str2num(cell2mat(u(1:counter,1)));
u_cr = str2num(char(u(1:counter,2)));
u_hg = str2num(char(u(1:counter,3)));
Now it works again. This is good for me since I'm just using numbers and floats, so char is safe. It would probably be bad with strings, though.





Community Treasure Hunt

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

Start Hunting!