Good day!
Please consider the following:
I want to generate a square wave using the Matlab function square()
in Simulnk using "Emebedded Matlab Function". I tried the same by using the eml.extrinsic
,
but I keep getting an error which states
'y'<the output from embedded function block> cannot be equated to square.
Please see following screen shots:
The idea behind this block is to generate a square wave based on the defined frequency range.
a
= amplitudef
= frequencydc
= duty cyclePlease let me know what I'm doing wrong? Or point me to what I have to read in order to understand my error? Or provide alternative methods to accomplish what I want.
Thanks in advance!
The compiler cannot determine the type and size of the outputs of extrinsic functions. Therefore, the compiler will be forced to keep it "MATLAB type" (AKA an mxArray
). The only thing you can do with an mxArray
in Embedded Matlab, is pass it onto another extrinsic function, but you cannot assign it to anything directly.
You'll have to tell MATLAB the type and size of the extrinsic function's output before calling the extrinsic function. You can do this by pre-alocating the variable with the same type and size of the (expected) output.
So, for your square wave:
function y = fcn(~)
%#eml
eml.extrinsic('square', 'linspace');
a = 1;
dc = 50;
f = 2*pi* (6908:1:9856);
% Pre-allocate
t = f;
% THEN assign
t = linspace(0, 1, numel(f));
% pre-allocate
y = t;
% THEN assign
y = a*square(f.*t, dc);