Simulink function giving an error of incorrect dimensions of matrix multiplication. Using MATLAB 2021b.
Show older comments
function op = fcn(ip)
mass =5 ; % mass of block
x = 0.3; y = 0.2; z = 0.1; % dimensions of block
Jx = (mass / 12) * (y^2 + z^2);
Jy = (mass / 12) * (x^2 + z^2);
Jz = (mass / 12) * (x^2 + y^2);
Jxz = 0;
J = [Jx 0 -Jxz; 0 Jy 0; -Jxz 0 Jz];
% Parse Input
OMG = ip(1:3) ;
EUL = ip(4:6) ;
TRQ = ip(7:9);
P_hi = EUL(1);
t_heta = EUL(2);
p_si = EUL(3);
P = OMG(1);
Q = OMG(2);
R = OMG(3);
OMGG = [0 -R Q; R 0 P; -Q P 0];
H = [1 tan(t_heta)*sin(P_hi) tan(t_heta)*cos(P_hi);
0 cos(P_hi) -sin(P_hi);
0 sin(P_hi)/cos(t_heta) cos(P_hi)/cos(t_heta)];
EUL_dot = H*OMG;
OMG_dot = J\ (TRQ - OMGG*J*OMG);
op = [OMG_dot; EUL_dot];
end
ERROR. Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To perform elementwise multiplication, use '.*'.

8 Comments
Walter Roberson
on 7 Aug 2023
is ip a row vector or a column vector? It would make a difference to the size of some of the variables.
John
on 7 Aug 2023
John
on 7 Aug 2023
Paul
on 7 Aug 2023
Does the error occur on the first simulation step or later?
You can debug a Matlab Function just like any other m-function. Set a breakpoint at the first executable line, and then check the dimensions of the variables on each line before stepping to the next line.
I'm surprised the error message doesn't give you the exact line number ....
This will be painful if the error shows up more than a few steps into the simulation.
Or does this error show up when you Update the model?
John
on 8 Aug 2023
Because the error occurs on the first step, just set a breakpoint at the first line and step through line by line checking dimensions as you go to see where the problem is, which will shed light on a solution.
And/or you might try explicitly setting the vector variables at the top of the code, like:
OMG = zeros(3,1);
(and similarly for the others) so that the code generator will know the dimensions, and then fill in the actual values as currently coded.
John
on 8 Aug 2023
Paul
on 8 Aug 2023
I'm afraid I'm out of ideas, at least for now.
Answers (0)
Categories
Find more on Dates and Time 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!