Step graph using a function block and scope in Simulink

203 Views Asked by At

I'm trying to to get a step graph from the following code (extracted from my main project):

Simulink model

Code:

function y = fcn(~)
    tic;
    y = 0
    pause(5);
    y = 1;
    pause(3);
    y = 0;
    pause(5);
    timeVal = toc;
    fprintf("Simulation time: %f",timeVal);
end

But I'm getting a flat line graph in the scope.

graph in scope

I'm always getting the flat line corresponding to the last assigned value. How do I get a plot for all the changes in the value of variable during execution?

1

There are 1 best solutions below

0
John Bofarull Guix On

1.-

The function that you have defined is not the way to define a MATLAB function inside the SIMULINK block MatlabFunction, at least not to measure the STEP RESPONSE of a SYSTEM on the SCOPE.

SIMULINK block MatlabFunction Help

SIMULINK Matlab function block properties

There's even an editor to generate SIMULINK Matlab block functions

MATLAB BLOCK EDITOR help page

While we can write MATLAB functions similar to what you have posted in your question, to

1.1.- Generate Step

1.2.- Feed Step to System

1.3.- Measure response

2.- Define a SAMPLE TIME

As it is, the scope does not have any time reference, because the function you have written does not have a time reference.

The easiest way to supply a time reference to the scope is to

PLUG AN INPUT SIGNAL TO THE FUNCTION BLOCK

An input signal that goes through the Matlab function block, generating and output that propagates the time reference supplied by the input signal.

For instance, consider using the step generator block suggested in the following video.

3.- WATCH THIS VIDEO explaining how to build a basic step response in SIMULINK.

https://youtu.be/_uPZx9iDfbc?feature=shared

4.- BUILD ONE LIKE THIS

enter image description here

5.- The Scope is flat because there is no input

Once you feed a step into the system, then the scope is going to have a signal to display.

6.- LEVEL-2 S-FUNCTION

Defining a level-2 S-function as explained here https://uk.mathworks.com/help/simulink/sfg/writing-level-2-matlab-s-functions.html

S-functions can be even written in C or C++ .

Once having followed these steps, if still in any doubt how to proceed, please do not hesitate mentioning it with and additional comment, to see what can be done.

Thanks for time and attention.