# Understanding MATLAB function blocks in Simulink

Before I ask my question, let me explain what I'm trying to do. I want to design a simple system in Simulink with the features shown below. When I say simple is that I don't need complex and large block systems. I just want to represent a basic behavior.

Features:

1. A way to turn the system on or off. It could be just a variable (1 or 0) to indicate whether is off or not.

2. After turning on the system, turn a valve on during 15 seconds and then turn it off.

3. After the 15 seconds, give the setpoint level to a water tank system. Turn water pump on.

4. When the level setpoint is reached, turn a burner on until the pressure setpoint is reached.

5. Always checking important variables such as pressure and level to make some kind of decision like turn a valve on or off.

I was trying to build a Matlab function in Simulink that decides what to do. For example:

I'm making the big mistake of thinking that I'm programming a microcontroller because I can't run loops inside this function and can't play with output variable values. I tried to use switch-case statements, while loops, etc, but it doesn't work like expected. For example, suppose I have the following matlab function in Simulink:

function y = fcn(u)
%#codegen

u=0;
x=10;
z=5;
y=x;
pause(10);
y=z;


The line

y=x;


Is never executed.

Basically I need some advice on how to do this. I'm a beginner in Matlab/Simulink. How can I effectively simulate a loop which continuously checks for inputs variables and updates output variables?