Accessing Block Data During a Simulation 6

Posted by Guy Rouleau,

This week I discovered that event listeners also work with bus signals!

You might be saying: "What is an event listener?"

In this post, I will describe how to register an event listener for a bus signal to display signal values on a figure. Here is the final result:

Example Event Listener

You can download the files for my event listener example here.

If you want to see a complete example that is really good, I recommend the File Exchange submission Simulink Signal Viewing using Event Listeners and a MATLAB UI by Phil Goddard. It has been selected as pick of the week some time ago.

Let's see how this stuff works.

Event Listener

An Event listener gives you a way to access block data during simulation. A typical use case is to display simulation data in a MATLAB GUI while a model is running. The key function you need to know about is add_exec_event_listener.

Setup a nonvirtual bus

Before setting up the event listener, we need to prepare a model. Let's create a simple test model including a bus with 2 elements:

Example model

We need to make the bus nonvirtual. For that, you can manually follow the steps outlined in Creating Nonvirtual Buses. Alternatively, here is a script that should do it automatically:

Example script making the bus nonvirtual

Registering the Event Listener

The event listener must be registered while the model is running using add_exec_event_listener. Because of this requirement, a good place to register the listener is in the model StartFcn callback. Here is what it looks like:

Registering an event listener in mdlStart callback

The callback function

In the image above, you can see that I register a function displayBusdata to execute after the block's Outputs method executes.

When it calls this function, the Simulink engine passes to it the Simulink.RunTimeBlock object for this block. This object allows you to access everything relevant to this block. For example, if you want to display the value of the bus elements at the MATLAB prompt, the registered function could be as simple as:

Simple listener function

If you want to go more complex and display the values in a MATLAB figure. Here is an example where I plot the sum of the two bus elements:

Simple listener function with a figure

Now it's your turn

I hope this inspires you to create nice MATLAB GUIs to interface with your simulations. Share your event listener stories by leaving a comment here.

6 CommentsOldest to Newest

Sean replied on : 1 of 6

This seems like a good example that would work well with either a persistent variable for “h”, or an application of nested function handles. Using either approach, you could avoid running “findobj” twice for each iteration.

I would assume you would be worried about optimizing the speed of this event code.

wei replied on : 2 of 6

@Guy, Could you discuss simulation performance with event listener? My past experience is that it slows down simulation quite a bit.

Further, is there way to profile such application, and handle graphics speed-up tips?

@Sean, thanks for the suggestion. I agree my code could be more optimal.

@Wei, event listeners definitely slow down simulations. The effect can be classified in the same category as MATLAB S-Functions. I do not recommend using this technique if simulation speeds is very important for you application.

I find that event listeners are useful to put a GUI on top of desktop simulations that run close to real-time and interact with it. In many cases, this type of simulation is slowed down using one of those blocks:

Sriniwas replied on : 4 of 6

First of all thanks for giving this demo of accessing the block data of a simulink model while the simulation is running in normal mode.

I was wondering whether the method of Event Listeners can be applicable for accessing the block data of a simulink model if the simulink model (build on Host PC) is running on a target PC with respect to xPC Target Environment.

Please give me your feedback

Yagiz replied on : 5 of 6

Thanks for this useful demo.
Like in the demo, I use a small script in model’s “start Fcn” to add event listener. When I try to add event listener to a subsystem I have an error that:

“Error evaluating ‘StartFcn’ callback of block_diagram ‘Myblockdiagram’ .
Caused by :
Can add listener only when block diagram is executing.”

I found that I get this error only when I try to add event listener to a subsystem. Otherwise it works. Do you know the reason of this problem.

I am using r2013.

@Yagiz: Event Listener can only be register for non virtual blocks. Once the model is running, the subsystem does not exist anymore, it is virtual. You can register the listener to the actual signal source inside the subsystem. Another option is to insert a Signal Conversion block after the subsystem and register the listener for this block.

Add A Comment

What is 2 + 5?

Preview: hide