How can I access Simulink signal data in App Designer during simulation?

14 views (last 30 days)
I want to live stream signal data and plot signal data in App Designer during simulation.
I could stream the data from Simulink to MATLAB by using the Data Access functionality explained in the following example:
However, I cannot use this data in App Designer because the callback function cannot return data. How can I access Simulink block data during simulation from App Designer?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 7 Aug 2025
Edited: MathWorks Support Team on 7 Aug 2025
You can access the signal data using "add_exec_event_listener":
"add_exec_event_listener" registers a listener for a block method execution event and returns a handle to the listener that it registered. For details on how to use this function, please refer to the following documentation.
Below is an example code that displays the data of signal 'x1' in the Edit Field. Please refer to the attached app file (example.mlapp).
function updateApp(app, block, event)   app.EditField.Value = block.OutputPort(1).Data; end function StartButtonPushed(app, event)   set_param(app.ModelName,'SimulationCommand','start')   app.Listener = add_exec_event_listener([app.ModelName '/x1'],'PostOutputs',@app.updateApp); end
If you want to access bus data composed of multiple elements, refer to the code below. Please refer to the other attached app file (example_bus.zip).
function updateApp(app, block, event)   app.EditField.Value = block.OutputPort(1).Data.x1;   app.EditField_2.Value = block.OutputPort(1).Data.x2; end function StartButtonPushed(app, event) set_param(app.ModelName,'SimulationCommand','start') app.Listener = add_exec_event_listener([app.ModelName '/Bus Creator'],'PostOutputs',@app.updateApp); end
Please note this API and using set_param/get_param to change block parameters is not supported in app deployment. If you eventually have the goal to deploy your app to a standalone executable, you will need to get Simulink Compiler and use the Simulink Compiler APIs for accessing signal data and modifying block parameters. 

More Answers (0)

Categories

Find more on Modeling in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!