1

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:

BLOCK IN SIMULINK Code for the said Block Error List

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.

Thanks in advance!

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot mate! I have a follwing question: 1. I cant even see one complete period in the scope. Any suggestions?
You are generating a signal of length 2948 at each time step, i.e. you are generating the whole square wave trace at once, and at every time step. Presumably you really want to generate a signal with length 1 at each time step, containing the value of the square wave at that time only. The easiest way to do that would be to generate the signal in MATLAB and then input into Simulink using a From Workspace block.
@PhilGoddard: thanks for the reply. and sorry for the late Acceptance, i didnt have a net connection this weekend. I have already tried using the SIM-IN block but always got an error which said NO TIME FRAME or SAMPLE TIME defined, that why i switched to embedded function. So if i have understood you right, you mean that i should define a [100x3] matrix where the first column is time and the other two are simply the values which i want? right??
@PhilGoddard: Also shouldnt this be done by the clock signal which i have included with the embedded function?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.