Scope display from Simulink within MATLAB GUI

8 views (last 30 days)
I'm looking to display live simulation data from a scope within a MATLAB figure/ UI I've created. I'm aware you're able to send Simulink data to the workspace and plot afterwards, however, I would like to see a 'live' plot in sync with the simulation.
I have a basic model which outputs variables such as 'speed', 'distance', 'fuel consumption' etc... and have made my GUI programmatically (not using GUIDE).
I'm aware 'event listeners' can be used to show 'live' simulation data within a GUI however I'm unsure of how to do this. I've seen an example which was rather complicated and difficult to follow.
I hope you can help! An example code would be very useful, I simply want to just display the data from some scopes I've set up within a GUI.

Accepted Answer

Sara Nadeau
Sara Nadeau on 2 Aug 2018
What version of Simulink do you have? If you have 18a, you can use the Data Access feature to specify a callback function to access logged signal data during simulation. You can write the callback function to update your MATLAB GUI.
This is a very simple example of a callback function that plots signal data in a MATLAB figure during simulation. The optional parameter is used to match the signal to its figure.
function plotSignals(y,time,sigNum)
figure(sigNum)
if isequal(sigNum,1)
marker = 'ro-';
elseif isequal(sigNum,2)
marker = 'go-';
else
marker = 'bo-';
end
hold on;
plot(time,y,marker);
end
To use this callback function to plot your signal data, right-click the logging badge for the signal you want to plot and select Properties. Specify the function name. This callback function uses simulation time, so you need to check the Include simulation time option. The function parameter is used to determine which plot data for a given signal should go to. With the Function parameter specified as 1, the signal x1 will be plotted on Figure 1 in red, according to the simple example callback function.
You can modify the callback function and the use of the optional parameter to suit your needs.
If you'd like to use event listeners, have you come across this blog post ?
Hope one of these options helps!
  2 Comments
Craig Saunders
Craig Saunders on 3 Aug 2018
Hi Sara,
Thank you for your answer, however I have v2016a so don't know if this is possible but i'll give it a try!
Do you have any solution for my version of MATLAB?
Many thanks,
Craig
Sara Nadeau
Sara Nadeau on 3 Aug 2018
Hey Craig,
For R2016a, I think your best bet may be the event listeners. Guy's blog post should help illustrate the process. The particular example deals with bus data. If your data isn't a bus, you can ignore the Setup a nonvirtual bus section. To access the block data, you would just need block.OutputPort(1).Data.
The other options for visualizing data during simulation are the Simulation Data Inspector and Dashboard blocks, including the Dashboard Scope block.
Prior to 17a, you need to mark signals specifically for streaming to the Simulation Data Inspector.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!