File Exchange

image thumbnail

Simulink Signal Viewing using Event Listeners and a MATLAB UI.

version (14.1 KB) by Phil Goddard
This demo shows how to use block listeners to view model signals in a MATLAB user interface.


Updated 02 Nov 2009

View Version History

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

This application demonstrates how listeners can be attached to the blocks in a Simulink model, and how those listeners can be used to display block inputs and outputs on a MATLAB user interface. (The main function for achieving this is called add_exec_event_listener.)

The model can be executed either as a standard simulation or after being built into a generic real-time target (for those users with an RTW license). In the later case, RTW's External Mode and TCP/IP is used to transfer data from the running executable to the model and thence to the UI.

The use of listeners to view signals rather than the traditional approach of writing a custom S-Function block has at least 2 advantages: firstly the model is not "corrupted" by having to add any special viewing block (this is particularly advantageous if the model is also to be used with RTW); and secondly the same UI could be used to view signals from different models.

Specifically this application works with a simple model called 'simpleModel.mdl', that contains the three blocks

Sine Wave --> Gain --> Scope

The UI allows the model to be started and stopped, and allows the value of the gain to be tuned. The value of the signal being fed into the Scope block is displayed on an axis on the UI. The model does not have to be open to use the UI (and it's recommended for it to be closed).

The UI allows the model to be run in either simulation mode (requiring a Simulink license) or as a generic real-time (GRT) executable (since the model must be "built" this requires an RTW license).

It is intended as a demonstration program to show various aspects of using MATLAB, Simulink and RTW:
- how to create a MATLAB UI using command line functionality
- how to start/stop a Simulink model using command line functionality
- how to add a listener to a Simulink block so that signals can be viewed from a MATLAB UI.
- how to build a GRT executable using command line functionality
- how to interface with code running in "real-time" (For the purposes of this UI the grt code is running on the host machine, so it is not running in hard real-time, however it is using External Mode to communicate with the code and hence shows how communication would be performed if the code was truly running on an RTOS.

Cite As

Phil Goddard (2021). Simulink Signal Viewing using Event Listeners and a MATLAB UI. (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (33)

Martin Neumann

Anusha K MTech

Franklin Ngo

@Phil Goddard: Could you please update the example for app designer? Thank you so much :)

xuejian han


thank you!


Thank you for sharing this great example!


Thank you for this great submission!
But: is there any way to speed the calculation time up?
The tool is very useful, but I would need it much faster...



When I tried to build the model I got this problem. I have Matlab R2014b. Please help how to resolve this issue.

gmake: *** [../simpleModel.exe] Error 48

C:\Users\DELL\Desktop\Matlab Help\Live Scope\simpleGUI_V3\simpleModel_grt_rtw>echo The make command returned an error of 2
The make command returned an error of 2

C:\Users\DELL\Desktop\Matlab Help\Live Scope\simpleGUI_V3\simpleModel_grt_rtw>An_error_occurred_during_the_call_to_make
'An_error_occurred_during_the_call_to_make' is not recognized as an internal or external command,
operable program or batch file.
### Build procedure for model: 'simpleModel' aborted due to an error.



Can this be made to work for subsystems?


Hi I'm trying to make a GUI to control a model on a RTW ,I made the GUI with GUIDE and set the strartfcn of the model to "localAddEventListener" , the model and GUI works perfectly on simulation but when I build it on XPC target box and try to press connect it gives me an error saying that it can't find the function "localAddEventListener"
can you please help me with that

zhou jianshan

Thank you for your sharing!


Hi!i'm italian and I hope I'm not wrong to write.First of all I want to thank you for this example.
I copied the functions localLoadModel = (modelName), localRemoveEventListener, localEventListener (block, evendata), modelLoaded=modelIsLoaded(modelName), localAddEventListener in my M-File , and the callback functin for the start button (Untitled_8 in my M-file) and stop button (Untitled_9 in my M-file)....but i have created my gui from GUIDE so i have copied this code in the Opening_Fcn:
% Load the simulink model
ad = localLoadModel(modelName);

% Put an empty line on the axes for each signal that will be
% monitored
% Save the line handles, which will be useful to have in an
% array during the graphics updating routine.
nlines = length(ad.viewing);
hl = nan(1,nlines);
colourOrder = get(handles.axes2,'ColorOrder');
for idx = 1:nlines
hl(idx) = line('Parent',handles.axes2,...
ad.lineHandles = hl;

% For the UI to be closed there needs to be a modelName field in
% appdata so just create a dummy one
ad.modelName = modelName;

% Create the handles structure
ad.handles = guihandles(handles.figure1);
% Save the application data

% Position the UI in the centre of the screen
% Make the UI visible

handles.output = hObject; %aggiunge un nuovo elemento alla struttura
%della GUI,assegnando il valore dell'hObject

% Salva la struttura handles
guidata(hObject, handles);

Matlab give me this error:
??? Reference to non-existent field 'lineHandles'.

Error in ==> oscilloscopio2>Untitled_8_Callback at 391
for idx = 1:length(ad.lineHandles)

Error in ==> gui_mainfcn at 96

Error in ==> oscilloscopio2 at 20
gui_mainfcn(gui_State, varargin{:});

Error in ==>

??? Error while evaluating uicontrol Callback


??? Reference to non-existent field 'eventHandle'.

Error in ==> oscilloscopio2>localRemoveEventListener at 792
for idx = 1:length(ad.eventHandle)

Error in ==> oscilloscopio2>Untitled_9_Callback at 417

Error in ==> gui_mainfcn at 96

Error in ==> oscilloscopio2 at 20
gui_mainfcn(gui_State, varargin{:});

Error in ==>

??? Error while evaluating uicontrol Callback

can you help me,please??


It works perfectly but I have a question.
In the Simulink model, I have a block including slash '/' (m/s), I want to obtain the signal from this block but when I write :
ad.viewing(1).blockName = sprintf('%s/m/s',ad.modelName);

The error is caused by the name, Matlab takes it like a subsystem because of the slash, is there a way to avoid this error ?

trega trega

Thank you for this example, it works very nice.
I would like to access both signals in XPGraph. To do that I changed

sTime = block.CurrentTime;

In localEventListener function.
However, now both variables: sTime and data have the same values. Could you tell me what am I doing wrong?

Vinol Joy D'souza

No problem....I self discovered the answers to both my questions

Vinol Joy D'souza

hi Phil and others,

I have a very silly problem.You have 2 graphs on your figure in the GUI.How can we add legend to this to identify the graphs.I will be very thankful if you help me in this regard.

Vinol Joy D'souza

You have changed the gain of the sine wave in this program...If i want to change the frequency on the GUI..What changes should i do???..

I would be grateful if you guide me through this...




Hi Mr.Phil, thank you for your example..Hereby wish to have your guidance on those the commands viewing(n) in your code is it according to the arrangement on the block in the simulink model? If my Display block is the 6th block , should i put the n as 6? Sorry that i asked stupid question as i am trying hard to understand the code for listener implementation. Other than that, can i have any relevant reference from you that is quite basic? I tried to google, but i hardly get a basic one... Mostly is the result from Mathwork...

thank you very much

Phil Goddard

A couple of comments ask about using the "old method" of an s-function instead of an event listener.
I often use the S-function approach, and to some extent prefer that way.
However it means that the model itself must be modified -- by the addition of the s-function block.

Sometimes, for whatever reason, you don't want to add that additional block.
Rather you want the UI to attach itself to an existing model, do its stuff, then unattach itself at the end.
That is what the event_listener approach allows.
e.g. you want to write a UI that allows the user to select one of many different models to run and upload data from.

Jose Goncalves

zanna z

what about "the old metod" s function??

Muawea Rawashdeh

First of all I want to thank you for this example.
But I would like to ask you some thing, I'm receiving a data from serial communication in a simulink file, then I'm sending these data to a function (m_file function) so it can calculate an angle from this data, then I want to display this angle in a GUI continually, I'm finding a problem in receiving this data in GUI. Is the event listener is the right way to do it? or I have to think in some thing else?
Thanks in advance for your help.


I am running simulink from a Matlab program and want to add a GUI. I have numerous programs in this format - so would prefer to keep the existing Matlab programs. I find that the GUI clears the Matlab parameters when the simulink model starts, e.g. t_stop, etc. Is there a simple way around this? E.g. set GUI parameters in the Matlab code before calling simulink?


Please, could you help how use event listener at Matlab Compiler ? I want to make web aplication, where i connect from web to simulink scheme by Matlab Compiler CGI or Java or .NET and i want to plot simulation output at real time. Is this possible ??

Phil Goddard

I've updated the example to show how to add listeners to multiple blocks.


I would first of all like to thank you for providing this example which helped me get a better understanding on how the add_exec_event_listener works in Simulink. Pardon me but I would like to ask you a question regarding this method. I am basically trying to acquire 2 output signals from OUT1 and OUT2 sinks within Simulink into a GUI. From what I've learned in your example, I've managed to get one or the other but not both AT THE SAME TIME which is my goal.

To put it into prospective using your example, I guess it would be like using add_exec_event_listener within 2 SCOPES trying to update 2 AXES in your GUI at the same time. My question is, can the add_exec_event_listener method work for outputting multiple block values into a GUI? I would really appreciate it if you could point me in the right direction or provide any advice.



Dennis Nord

Dennis Nord

Thank you for a great contribution. I am currently working on my degree project (which, among other things, involves creating simulink gui's for control design using realtime windows target and a NI Daq-card). This is very interesting, i will definitely give it a shot.

Do you suppose this solution would be faster or slower as opposed to the "old" s-function way? Im currently having problems with s-functions and 2 axes, 5 signals (line children). If i try to update the axes faster than about 10 times per second (i do this independently of sampling time using modulo operator) the GUI tends to "lag" after the simulation and the objects (i think the whole s-function?) gets an increasing time delay towards the actual process (although the control system seems to run well!)

Am i correct if i assume the event listener will trigger on each step in the simulink model (thats how fast i sample my signals as well)? that is, if i have a step size of 0.001 the listener will trigger at a rate of 1 kHz? Is there a good way to downsample this without interfering with the simulink model?

Phil Goddard

The reported bug about not being able to have the model open _before_ opening the UI has been fixed.

Note however that there are many model-UI interactions that the demo does not handle. For instance if the simulation is started from the UI, but is stopped by pressing the stop button on the model, then UI stop/start buttons are not updated. There are many such interactions that would need to be handled in a production version of this app, but are not handled here since they are not core functionality for the topic at hand.

Arnaud Miege

This is a very efficient yet simple solution to a commonly asked question. If the Simulink model is open before opening the GUI in MATLAB, the following error message is displayed:

"A Simulink license isn''t available, or cannot be checked out. The UI is being rendered however all functionality is being disabled. Check for an available llicense then try again."

which is clearly not the case. If you then try to close the GUI, you get the following error message:

"??? Reference to non-existent field 'tuning'.

Error in ==> simpleGUI>localCloseRequestFcn at 536

??? Error while evaluating figure CloseRequestFcn"

It seems like you can't have the Simulink model open when running the GUI.

MATLAB Release Compatibility
Created with R2008b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!