error in Function output: cannot be of MATLAB type

3 次查看(过去 30 天)
Hello,
I develop a function to input four components, which allows for a signal blocking over time according to a codition, I proceed as follows:
function [y1,y2] = fcn(u)
%#codegen
coder.extrinsic('degtorad');
coder.extrinsic('radtodeg');
Az=0;
El=0;
In=0;
Or=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)
j=1;
end
X2=cos(El(n))*cos(Az(n));
X3=sin(Az(n))*cos(El(n));
I=cos(In(j))*sin(El(n))-sin(In(n))*X2*cos(Or(j))-sin(In(j))*X3*sin(Or(j));
if (0.9<I && I<=1)
y1=radtodeg(In(j));
y2=radtodeg(Or(j));
else
j=n-1;
I=cos(In(j))*sin(El(n))-sin(In(n))*X2*cos(Or(j))-sin(In(j))*X3*sin(Or(j));
y1=radtodeg(In(j));
y2=radtodeg(Or(j));
end
when i run it, i have this error:
Function output 'y1' and 'y2' cannot be of MATLAB type.
can u help me to resolve it?

回答(1 个)

Walter Roberson
Walter Roberson 2015-5-23
http://www.mathworks.com/help/simulink/ug/calling-matlab-functions.html and scan down to "Converting mxArrays to Known Types"
In summary: initialize y1 and y2 to 0 before assigning values to them that are the outputs of function calls.
  8 个评论
Walter Roberson
Walter Roberson 2015-5-23
If you need to store an unknown number of values in Simulink, then there are a few approaches.
The first approach is to put a maximum limit on the number of elements to be stored, and initialize the vectors accordingly. As you go, you keep track of how many elements have actually been used. At any one time, you then use subscript indexing like Az(1:numused) to refer to the portion that is meaningful. Eventually you will either finish normally or you will get to the limit of the maximum number of elements you allocated and you need to stop processing. Then for all of the values that are being returned as signals, just before returning, assign to them the subset that was actually used, such as
Az = Az(1:numused);
This is okay because in Simulink you can shrink an array but you cannot grow it larger than you initialized it.
The second approach is like the first in having hard-coded vector sizes, but does not require that the variable be initialized to its longest possible value and does not require that you subscript by the number of elements currently in use. Simulink effectively does those things for you if you set up the variable using coder.varsize
The third approach is to use dynamic memory allocation. I think this might involve using coder.varsize as well, but I am not able to access the documentation as I do not have the license or software support agreement. You could try looking starting at http://www.mathworks.com/help/fixedpoint/ug/minimize-dynamic-memory-allocation.html
studentU
studentU 2015-5-24
thank you Roberson for repply,
i used the second approch, i've add at the begining coder.varsize('Az','El','In','Or'); but it generates this error:
Variable 'k' is undefined on some execution paths.
I can not declare the k before, if (n == 1) because I want the exchange k values if I declare, it will take the initial value each TE

请先登录,再进行评论。

类别

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