Effacer les filtres
Effacer les filtres

Using Embedded Matlab Function

11 vues (au cours des 30 derniers jours)
Sheetansh Kaushik
Sheetansh Kaushik le 31 Mai 2013
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.

Réponse acceptée

Sheetansh Kaushik
Sheetansh Kaushik le 3 Juin 2013
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!

Plus de réponses (1)

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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Simulink Functions dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by