MATLAB Function Block - output cannot change

Hi All
I am trying to generate different output by using MATLAB Function Block. Below is my code, simulink model and output measure by scope.
function duty = fcn()
duty = 0.5;
pause(1);
duty = 0.1;
pause(1);
end
simulink model.jpg
scope.jpg
However, the output of the function block always come from the last value. Anything wrong about my code?

3 Comments

The only output from a Matlab function block that will be sent to Simulink is the value of the output when the function exits. In this case that would be 0.1. The intermediate internal value of 0.5 will never be sent as an output signal to Simulink and the pause instructions do nothing but slow down your simulation.
If you want to generate a signal that is 0.5 for one second and then 0.1 for 1 second, you might want to look at the Signal Builder block or other, simpler, options (such as a square wave of magnitude +/-0.2 with a constant block of 0.3 and a sum block to generate the offset, with appropriate frequency)
I suggest going through some of the Simulink tutorials in the Matlab documentation to help you get started with how Simulink operates on signals.
Hi, thank for your reply. Actually I am trying to write an algorithm for my simulink model. I am thinking to use MATLAB block to implement it. The above example is just my experiment. My algorithm have to real time read different data from excel files and generate corresponding different outputs. Anyway suggestion from you how to implement it? Thank you.
In Simulink, a MATLAB Function Block can emit outputs only when it returns. The outputs returned can potentially be vectors or arrays, and are not necessarily fixed size.
However, if you return N output values from a single Function Block invocation, how do you want those treated in time? Do you want to queue those N values in a bunch, and release one value per time-step? Do you want to process all N of the values in a group in the same time step? Do you want a series of blocks to suddenly run N times faster so that at the end of the time step for the Function Block, all N of the outputs have been processed one at a time?

Sign in to comment.

Answers (0)

Products

Release

R2019a

Asked:

on 22 Dec 2019

Commented:

on 23 Dec 2019

Community Treasure Hunt

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

Start Hunting!