Error in port widths or dimensions

3 次查看(过去 30 天)
Hi, I do get the following error:
Caused by:
Error using sldebugui (line 77)
Errors occurred during parsing of MATLAB function 'Line force/MATLAB
Function'(#229)
Error using sldebugui (line 77)
Error in port widths or dimensions. Output port 1 of 'Ceto5_model_028/Line force/MATLAB Function/x' is a one dimensional vector with 1 elements.
My matlab embedded function is the following:
function [Fx, Fz, M, alpha1, alpha2] = Line(x, z, beta, xo, zo, k, x_ba, z_ba) %#codegen
lo = ((xo-x_ba)^2+(zo+z_ba)^2)^0.5;
zb1 = z + z_ba*cos(beta) + x_ba*sin(beta); zb2 = z + z_ba*cos(beta) - x_ba*sin(beta); xb1 = x - x_ba*cos(beta) + z_ba*sin(beta); xb2 = x + x_ba*cos(beta) + z_ba*sin(beta);
alpha1 = atan2(xb1,zb1); alpha2 = atan2(2*x0-xb2,zb2);
l1 = sin(alpha1)*zb1; l2 = sin(alpha2)*zb2;
F1 = max(0,k*(l1-lo)); F2 = max(0,k*(l2-lo));
Fx1 = -F1*cos(alpha1); Fx2 = F2*cos(alpha2); Fz1 = -2*F1*sin(alpha1); Fz2 = -2*F2*sin(alpha2);
Fx = Fx1 + Fx2; Fz = Fz1 + Fz2; M = Fx1*(zb1-z) - Fz1*(xb1-x) + Fx2*(zb2-z) - Fz2*(xb2-x);
My input x is just a single value, I'm not sure why I get this error, can anyone help? Thanks Alex
  2 个评论
Mike Hosea
Mike Hosea 2014-9-15
编辑:Mike Hosea 2014-9-15
Formatting the code for easier reading:
function [Fx, Fz, M, alpha1, alpha2] = Line(x, z, beta, xo, zo, k, x_ba, z_ba) %#codegen
lo = ((xo-x_ba)^2+(zo+z_ba)^2)^0.5;
zb1 = z + z_ba*cos(beta) + x_ba*sin(beta);
zb2 = z + z_ba*cos(beta) - x_ba*sin(beta);
xb1 = x - x_ba*cos(beta) + z_ba*sin(beta);
xb2 = x + x_ba*cos(beta) + z_ba*sin(beta);
alpha1 = atan2(xb1,zb1);
alpha2 = atan2(2*x0-xb2,zb2);
l1 = sin(alpha1)*zb1;
l2 = sin(alpha2)*zb2;
F1 = max(0,k*(l1-lo));
F2 = max(0,k*(l2-lo));
Fx1 = -F1*cos(alpha1);
Fx2 = F2*cos(alpha2);
Fz1 = -2*F1*sin(alpha1);
Fz2 = -2*F2*sin(alpha2);
Fx = Fx1 + Fx2;
Fz = Fz1 + Fz2;
M = Fx1*(zb1-z) - Fz1*(xb1-x) + Fx2*(zb2-z) - Fz2*(xb2-x);
Image Analyst
Image Analyst 2014-9-15
Exactly which of those lines is line #77. Also it's not a good idea to name your own functions after built in MATLAB functions like line(). True, MATLAB is case sensitive and Line() is different than line() but nonetheless it's not a good idea.

请先登录,再进行评论。

采纳的回答

Mike Hosea
Mike Hosea 2014-9-15
OK, x is just a scalar. Now how about z, beta, xo, zo, k, x_ba, and z_ba? Assuming they are all scalars, when I run this code in MATLAB, I get
>> [y1,y2,y3,y4,y5] = Line(1,2,3,4,5,6,7,8) Undefined function or variable 'x0'. Error in Line (line 8) alpha2 = atan2(2*x0-xb2,zb2);
Perhaps this should be xo?

更多回答(1 个)

Alexandre
Alexandre 2014-9-15
Thanks Mike, You're right, it should be xo and not x0, stupid mistake! Also I found out that the output variable need to be initialised at the start of the script: Fx = 0; Fz = 0; M = 0; alpha1 = 0; alpha2 = 0;
CHeers Alex
  2 个评论
Image Analyst
Image Analyst 2014-9-15
It's bizarre though that you got the error message you did instead of the one Mike got. Weird.
Mike Hosea
Mike Hosea 2014-9-15
编辑:Mike Hosea 2014-9-15
Although we encourage people to test their code in MATLAB, a lot of code gets written for the MATLAB Function Block that never gets run in MATLAB. Often the compiler will issue a similar error to what would have been encountered in interpreted mode. I think the error was probably reported in this case, too, but the Simulink error was the first error, and most of us have been trained by experience to deal with the first error first, since at some point after that, compilers tend to generate spurious "downstream" errors. In this case, unfortunately, the Simulink error is actually a "downstream" error, so we'll have to take some of the blame here. :) Consequently, I have just created a bug report for this, although I'm not sure we will be able to fix this without creating some other problem in a different situation.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB Coder 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by