error using global to save value of parameter

5 次查看(过去 30 天)
hi,
how can i resolve this error; Global declaration not resolved to a Data Store Memory block registered via the Ports and Data Manager.
my code is:
function [y1, y2] = fcn(u)
%#codegen
coder.extrinsic('degtorad');
coder.extrinsic('radtodeg');
coder.varsize('Az','El','In','Or');
global k
Az=0;
El=0;
In=0;
Or=0;
n=0;
%k=0;
y1=0;
y2=0;
Azk =degtorad( u(1));
Elk= degtorad(u(2));
Ink=degtorad(u(3));
Ork=degtorad(u(4));
Az = [Az ; Azk];
El= [El ; Elk];
In=[In; Ink];
Or=[Or; Ork];
n=size(Az,1);
if (n==1)
k=1;
end
X2=cos(El(n))*cos(Az(n));
X3=sin(Az(n))*cos(El(n));
I=cos(In(k))*sin(El(n))-sin(In(n))*X2*cos(Or(k))-sin(In(k))*X3*sin(Or(k));
if (0.9<I && I<=1)
y1=radtodeg(In(k));
y2=radtodeg(Or(k));
else
if (n>1)
k=n-1;
I=cos(In(k))*sin(El(n))-sin(In(n))*X2*cos(Or(k))-sin(In(k))*X3*sin(Or(k));
y1=radtodeg(In(k));
y2=radtodeg(Or(k));
end
end

回答(1 个)

Walter Roberson
Walter Roberson 2015-5-31
You use the Ports and Data Manager to define the global variable. See http://www.mathworks.com/help/simulink/ug/using-global-data-with-the-matlab-function-block.html
  2 个评论
Walter Roberson
Walter Roberson 2015-5-31
Reminder: As discussed in http://uk.mathworks.com/matlabcentral/answers/218308-error-in-function-output-cannot-be-of-matlab-type it is not possible for n to ever be 1 in your code, so your code will not assign a value to k before it is used.
With the flow of your code, if you do use k as a global, then the first time you execute the block, k will use whatever initial value you assigned in the Ports and Data Manager, and if the first condition is satisfied based upon the inputs, then you do not change k and so it will continue to be whatever that value was. But if the first condition is not satisfied based upon the inputs, then you will change k to n-1, and since n is the length of the something that is always two elements long in your code, k will be changed to 2-1 = 1. As it is a change to a global variable, that value will be remembered for the next time you enter the block. Once it has become 1 in this way, further iterations of the block will either leave it untouched (if the condition holds for their input) or else it they will re-change it to n-1 which will still be 2-1.
Therefore, whatever initial value you assign in the Data and Ports Manager will be used until the first time the condition fails on input; after that the value will be set to 1, and there is no way to change it to anything else in your code.
The condition is therefore acting as a trigger, so you should be considering coding it as a trigger. You should be considering selector blocks with two different versions of the code, one of which knows the trigger is not set but might update the trigger, and the other knows the trigger is already set and does not update it.
studentU
studentU 2015-6-2
thank's Roberson for ur replly, sorry, i have just see it...
so in my code it's sur that the first condition is always verified at the begenin because, the input parametrs has before their injection, calculated with this condition...
howerver adding data stor memory for 'k' in my model have not modified the error!!!

请先登录,再进行评论。

类别

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

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by