Using Embedded Matlab Function

8 次查看(过去 30 天)
Good Day!
Please consider the following:
I want to generate a Square wave using the Matlab function: sqaure(),in Simulnk using "Emebedded Matlab Function". I tried the same by using the 'eml.extrinsic'; But i keep getting an error which states :- Function output 'y' cannot be of MATLAB type. Please see following code:
function y = fcn(~)
%#eml
eml.extrinsic('square');
a=1;
dc = 50;
for i=1:100
f=(6908:1:9856)';
f=f*2*pi;
t=0:0.5:22;
y=a*square(f*t,dc); %ERROR
end;
THe Embedded Function Block has no input; only 1 output and a Clock trigger. I am not able to upload images. SOrry for that.
The idea behind this block is to generate a square wave based on the defined frequency range. •a = amplitude •f = frequency •dc = duty cycle
Please 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.

采纳的回答

Sheetansh Kaushik
Consider the following code:
function y = fcn %#eml
eml.extrinsic('square', 'linspace');
a = 1; dc = 50; f = 2*pi* (6908:1:9856);
t = f; % pre-allocation of f
t = linspace(0, 1000000, numel(f)); % re-assignment
y = t; % pre-allocation of t
y = a*square(t.*f, dc); % element wise multiplication and Then final assignment
If you will notice here, the pre-allocation solves the problem of difference in datatype. 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.
---This answer was given to me!!--- i didnt come up with it!! Hope it helps people!

更多回答(1 个)

Kaustubha Govind
Kaustubha Govind 2013-5-31
Please see this previously answered question: http://www.mathworks.com/matlabcentral/answers/46993
  1 个评论
Sheetansh Kaushik
Thanks a lot for guiding me! i have found a detailed answer, which i will upload in sometime!

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by