You are on page 1of 3

Explaining the development of a GUI with Event Listeners.

Luiz Kressin and Rafael Marengo


18/07/2016

Introduction

This report objective is to explain how a MATLAB GUI can be used to interact with Simulink, changing
block values and visualizing data, using as model a heating tank being controlled by a PID Controller.

1.1

The Model

The versality and easiness to create a Simulink Model is very appreciated. The idea is that the software
can be easily adapted to another model or objective.
The model used is shown on Figure 1.
The heating tank has four inputs: There is the temperature of the heat used (Heat Temp.) and its flow
rate (HeatInputFlowRate). Also, there is the input temperature of the water being heated (TankInputTemp) and its flow rate (TankInputFlowRate).
This lead to two outputs: The temperature of the heat after passing through the tank (HeatTempOutput)
and the temperature of the water inside the tank (TankTempOutput).

Figure 1: Simulink Model - Heating Tank.


where:
1. SP is the Set Point when the model is running on Auto Mode. MV is the heat temperature when
the model is running on Manual Mode.
2. The switch alter between the controlled and manual mode.

How to change a Simulink Block value

It is necessary to know the name of the parameter that we are going to change. This can be achieved
using the following code.
1
2
3
4
5

% load the Simulink Model .


load_system ( N a m e O f Y o u r S i m u l i n k M o d e l ) ;
% shows the parameters of a specific block .
get_param ( ' N a m e O f Y o u r S i m u l i n k M o d e l / Block ' , ' DialogParameters ' )
Then, the code to change the value of a blocks parameter:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

% Changing the value of a Simulink Block .


% First of all , it is necessary to load the simulink model at the
Opening
% Function of the GUI .
handles . ModelName = ' N a m e O f Y o u r S i m u l i n k M o d e l ' ;
load_system ( handles . ModelName ) ;
% Then , it is possible to change the parameter of an specific block .
% Assuming that there is a Switch on the model .
% holds the value that is going to be loaded into the switch .
Sw it ch In it ia lS ta te = 1;
% First , it is pointed which block we are going to change .
% Then , which paramter of that block we are interested to change .
% After , the value . Usually the value needs to be a string .
set_param ( ' N a m e O f Y o u r S i m u l i n k M o d e l / Switch ' , ' sw ' , num2str (
S wit ch In it ia lS ta te ) ) ;

How to add an Event Listener to a Simulink Block

The code explaining how to add an event listener to a block:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

% Adding a event listener .


% First , it is necessary to create a function that is going to be called
% when the simulation starts . This function will add the event listener
% to an specific block .
function [ EventHandleSwitch , EventHandleGain ] = l o c a l A d d E v e n t L i s t e n e r
handles = guidata ( gcbo ) ; % get the data
% Let ' s assume that the name of the Simulink Model is ' Model '
% First , we define which block are we going to add a listener .
% Then , we define the block events we are going to register .
% After , we define the function that will be called every time an event
% occurs .

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

Eve ntHandleSwit ch = a d d _ e x e c _ e v e n t _ l i s t e n e r ( ' Model / Switch ' , ...


' PostOutputs ' , ...
@( block , eventdata ) l oc a l Ev e n tL i s te n e r1 ( block , eventdata , handles ) ) ;
EventHandleGain = a d d _ e x e c _ e v e n t _ l i s t e n e r ( ' Model / Gain ' , ...
' PostOutputs ' ,...
@( block , eventdata ) l oc a l Ev e n tL i s te n e r2 ( block , eventdata , handles ) ) ;
guidata ( gcbo ) ;
% At our simulation , a Start button is used to start the simulation .
% When this button is pressed , the function ' localAddEventListener '
% is added to the StartFcn of Simulink using the following line .
set_param ( ' N a m e O f Y o u r S i m u l i n k M o d e l ' , ' StartFcn ' , ' l o c a l A d d E v e n t L i s t e n e r ' )
;
% Also , it is necessary to create a function that will be called every
time
% an event occurs . This is used to plot a ' real time ' graph .
% Associated to Switch Block .
function l o c al E v en t L is t e ne r 1 ( block , eventdata , handles )
% %% Do whatever
% Associated to Gain Block .
function l o c al E v en t L is t e ne r 2 ( block , eventdata , handles )
% %% Do whatever

Conclusion

The objective of the project led to the understanding of how it is possible to interact Simulink and
GUI, as shown above. Therefore, the knowledge obtained through this software can be easily adapted
to another model or objective.

You might also like