How do I plot Simulink simulation data in App Designer during simulation?

31 views (last 30 days)
I am using App Designer to develop an app that controls a Simulink model. I would like to know if it is possible to plot Simulink simulation data in App Designer during simulation, similar to the Scope block.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 20 Feb 2023
Scope block cannot be included in App Designer at this time. To plot data on UIAxes in a way similar to the streaming scope, you will need to obtain data from Simulink using “add_exec_event_listener” and write code. Here are the steps:
1. Get Simulink block data using “add_exec_event_listener”. You can refer to this answer for more information:
2. To plot the data in UIAxes, you will need functions that trim the data obtained from step 1 and update lines. Below is an example of how the functions work. Please see the attached file for an example App Designer file and Simulink model.
function clipSignalTraces(app) % Get(trim) Signal Data
maxSigLen = 5000;
startX = 0;
for sigIdx = 1:length(app.signalTraces)
hLine = app.signalTraces(sigIdx);
xd = get(hLine,'XData');
sigLen = length(xd);
if sigLen > maxSigLen
startEl = sigLen-maxSigLen+1;
if xd(startEl) > startX
startX = xd(startEl);
end
end
end
if startX == 0, return; end
for sigIdx = 1:length(app.signalTraces)
hLine = app.signalTraces(sigIdx);
xd = get(hLine,'XData');
yd = get(hLine,'YData');
elIdx = find(xd <= startX);
xd(elIdx) = [];
yd(elIdx) = [];
set(hLine,'XData',xd, 'YData', yd);
end
end % clipSignalTraces
function updateSignalTrace(app,sigTag,time,data) % Update signals in UIAxes
for sigIdx = 1:length(app.signalTraces)
hLine = findobj(app.signalTraces(sigIdx),'flat','Tag',char(sigTag(sigIdx)));
assert(~isempty(hLine));
xData = [hLine.XData time];
yData = [hLine.YData data(sigIdx)];
set(hLine, 'XData',xData, 'YData', yData);
end
end % updateSignalTrace

More Answers (0)

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!