Embedded matlab coder error

2 次查看(过去 30 天)
studentU
studentU 2016-7-28
Hello, I am interesting to simulate the following function in Embedded Matlab coder (model.slx):
function U = Func1 (C,E,F)
gt = coder.nullcopy(zeros(3,20));
U = coder.nullcopy(zeros(3,20));
for i=1:19
for j=1:20
gt(:,j)=EK(:,j)-Fk(:,j)-Uk(:,j)
U(:,j)=E(:,j)-F(:,j)-C(:,j)-gt(:,j);
Ek = [Ek , E(:,i)];
Fk= [Fk , F(:,i)];
Uk= [Uk , U(:,i)];
end
end
end
While Uk, Ek, and Uk are initialised in init.ma:
Fk=[0;0;0];
Uk=[0;0;0];
Qk=[0;0;0];
open_system(model);
when I compile init.m, the following errors are generated:
1/- Dimension 2 is fixed on the left-hand side but varies on the right ([3 x 20] ~= [3 x :?]). for 'Uk' 'Qk' and 'Ek'
2/- Undefined functions or variables 'Uk' 'Qk' and 'Ek'. The first assignment to a local variable determines its class.
I would greatly appreciate any sort of guidance in solving this problem.
Thank you.

回答(1 个)

Walter Roberson
Walter Roberson 2016-7-28
Do not grow arrays incrementally in Embedded Coder: allocate the full size for them and fill them in.
You might need to assignin('base') for your variables; I am not at all sure that would be enough though.
Your code is not clear as to whether you expect those three variables to be "global": when you expand the variables inside the loop, are you expecting to always start with the 3 zeros each time the function is invoked, or are you expecting that the changed arrays will persist for the next execution of the function? If you are expecting the changed values to persist then you should be using "global" for the variables (and notice that your arrays would continue to grow because you always add on to the end of them no matter how big they started out.)
I suspect what you should be doing is using a From Workspace block to import the variables as signals and then include those signals as input ports to the function block. I am not sure whether you would also want a To Workspace block to save the variables afterwards; I suspect not.
  4 个评论
studentU
studentU 2016-7-28
Thank's,
for the last error, it's in other part of model, where i use a loop for as following:
for i=1:k
x1=px(i);
x2=py(i);
x3=pz(i);
p=[x1;x2;x3];
f(:,i)=myfuc(p);
end
the obtained error:
MATLAB function eML_blk_kernel in MATLAB Function 'x1=px(i)'
Walter Roberson
Walter Roberson 2016-7-28
There had to have been a line before that in the error message. For example the line might have said something like
Attempted to access 1 element of data px whose runtime size is 0.
If that happens then it is because px is empty. A global variable that has not been initialized would be empty.

请先登录,再进行评论。

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by