Simulink Signal Viewing using Event Listeners and a MATLAB UI.

This demo shows how to use block listeners to view model signals in a MATLAB user interface.
15.8K Downloads
Updated 2 Nov 2009

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 (2024). Simulink Signal Viewing using Event Listeners and a MATLAB UI. (https://www.mathworks.com/matlabcentral/fileexchange/24294-simulink-signal-viewing-using-event-listeners-and-a-matlab-ui), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2008b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on View and Analyze Simulation Results in Help Center and MATLAB Answers

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes
1.2.0.0

Version 3 gives an example of how to add listeners to multiple blocks. (Read the help for the m-file to see why this works differently in simulation and external mode.)

1.1.0.0

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