simulink matlab function block error

조회 수: 18 (최근 30일)
Zhengyuan Wang
Zhengyuan Wang 2015년 8월 4일
편집: Mike Hosea 2015년 8월 4일
Hello guys,
Im trying to calculate my lqr gains online, im having some troubles with the simulation, i get the error message saying
Function output 'Kr' cannot be an mxArray in this context. Consider preinitializing the output variable with a known type.
Errors occurred during parsing of MATLAB function
Here is the code, I am trying to obtain the Kr(1,1) in this case.
% code
function Kr = fcn(u)
%#codegen
eml.extrinsic('lqr');
a = 1.3629;
b = 1.2996;
Cf = 16000;
Cr = 16000;
Cr2 = 15000;
I = 2761;
l = a+b;
m = 2024.65;
w = 2e-7;
a11 = -2*(Cf+Cr)/(m*u);
a12 =(2*(b*Cr - a*Cf)/(m*u))-u;
a21 = 2*(b*Cr-a*Cf)/(I*u);
a22 = -2*(b^2*Cr+a^2*Cf)/(I*u);
b1 = 0;
b2 = 1/I;
A = [a11 a12; a21 a22];
B = [b1; b2];
Q = [0 0; 0 1];
R = [w];
Kr = lqr(A,B,Q,R);
end

채택된 답변

Mike Hosea
Mike Hosea 2015년 8월 4일
You need to tell the compiler what the output of lqr will look like.
Kr = zeros(1,2); % Define the output type for the extrinsic call.
Kr = lqr(A,B,Q,R);
Then you can do what you want with Kr.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2015년 8월 4일
Change it to
tKr = lqr(A,B,Q,R);
Kr = tKr;
  댓글 수: 4
Walter Roberson
Walter Roberson 2015년 8월 4일
Because the usual reason that this error occurs is that you have called a MATLAB routine and passed the output directly onwards to another routine, and the usual fix is to assign the value to a variable and use the variable.
Mike Hosea
Mike Hosea 2015년 8월 4일
편집: Mike Hosea 2015년 8월 4일
Thanks. I understand where you were coming from now. The tidbit that you're missing is that this variable needs already to have been defined (and to have the right type and size). It can't appear first as the output of an extrinsic function. The compiler will use what it knows about the size and type of the variable to copy the data out of the mxArray into "native" data types before continuing on.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Simulink Functions에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by