How to plot a Simulink Desktop Real-Time model output in an App Designer app?

I need to create an app that runs a Simulink Desktop Real-Time model and plots the output in the app. The model has a Analog Input block connected to a scope. For having the signal output to the workspace, I configured the scope properties --> logging and checked 'log data to workspace'.
In the app, I am having an UIAxes for plotting the simulation output.
Eventhough I am capable of running the model using 'set_param' command, I am unable to plot the output in the UIAxes.
Any information will be helpful and thanks in advance.

 Accepted Answer

Hi Nagendra,
There are different ways to acheive and one solution you can find in the below link:
let me know if you are looking for this

4 Comments

Dear Mr Ankit,
I used a similar approach that you provided, I get zero errors, but unable to get the plot updated into the app axes. I will explain my problem in detail below,if possible please help me.
This is my Simulink model and app with code.
Simple Simulink model (Name:AppDesignSetup.slx) with Out port added for callback.
The callback added in the Model Properties.
The App layout with axes tag 'plotchart' given directly in the identifier properties.
The callbacks for start and stop buttons
methods (Access = private)
% Button pushed function: StartButton
function StartButtonPushed(app, event)
load_system("AppDesignSetup.slx")
set_param('AppDesignSetup/Sine', 'Amplitude', ...
num2str(app.AmplitudeEditField.Value));
set_param('AppDesignSetup', 'StopTime',num2str(app.DurationEditField.Value));
set_param('AppDesignSetup', 'SimulationMode', 'normal');
set_param('AppDesignSetup', 'SimulationCommand','start');
end
% Button pushed function: stopButton
function stopButtonPushed(app, event)
set_param('AppDesignSetup', 'SimulationCommand','stop');
end
end
The Listner function code
function varargout = plotApp(varargin)
hf = findall(groot, '-property','tag' );
ht = get(hf, 'tag');
[a,b] = ismember('plotchart',ht);
if a
plotting = hf(b);
end
rto1 = get_param( 'AppDesignSetup/Out1','RuntimeObject');
Y1 = rto1.InputPort(1).Data;
X1 = rto1.CurrentTime;
%get_param('AppDesignSetup','SimulationTime');
plot(plotting, X1,Y1,'r','.');
hold(plotting, 'on');
end
when I run this the App axes dont get updated, So, I tired debugging with break point at plot line and here is the workspace screenshot
If possible please provide me a solution, if any info needed please let me know.
why X1 and Y1 have just one value?
And you have not included the following part of code in StartButtonPushed(app, event)
app.Out1.Tag = 'Out1';
app.Out2.Tag = 'Out2';
app.UIAxes.Tag = 'UIAxes';
cla(app.UIAxes);
Please add and let me know if it works for you!
Actually I am not having a clear idea of why X1 and y1 have only one value. I am thinking, once I have a proper running model that posts aleast the point (X1,Y1) into the app, then I will look into the line plotting.
Coming to Tag, the reason for not including it is I have given the in the UIaxes property section.
But after you mentioned it I created a similar new app and simulink model and tried the way you mentioned it, also added the missing lines like persistent and drawnow commands.
Here is the updated model: The simulink model is similar
the App button callback
app.UIAxes.Tag = 'plotAxes';
load_system('sineTest');
set_param('sineTest/Sine Wave', 'Amplitude', num2str(app.AmplitudeEditField.Value));
set_param('sineTest', 'SimulationMode','normal');
set_param('sineTest', 'SimulationCommand','start');
The Lister function code.
function varargout = plotApp(varargin)
persistent plotting
hf = findall(0, '-property','tag' );
ht = get(hf, 'tag');
[a,b] = ismember('plotAxes',ht);
if a
plotting = hf(b);
end
rto1 = get_param( 'sineTest/Scope','RuntimeObject');
Y1 = rto1.InputPort(1).Data;
X1 = get_param('sineTest','SimulationTime');
plot(plotting, X1,Y1);
hold(plotting, 'on');
%guidata(hf)
drawnow
but still the same result, that I dont see any plot or point in app axes. Are yo able to find any mistakes? Also Im including the debugging screenshot
function varargout = plotApp(varargin)
persistent plotting
hf = findall(0, '-property','tag' );
ht = get(hf, 'tag');
[a,b] = ismember('plotAxes',ht);
if a
plotting = hf(b);
end
rto1 = get_param( 'AppDesignSetup/Scope','RuntimeObject');
Y1 = rto1.InputPort(1).Data;
X1 = get_param('AppDesignSetup','SimulationTime');
plot(plotting, X1,Y1,'.');
hold(plotting, 'on');
%guidata(hf)
drawnow
App Code View:
function StartButtonPushed(app, event)
app.UIAxes.Tag = 'plotAxes';
cla(app.UIAxes);
load_system("AppDesignSetup.slx")
set_param('AppDesignSetup/Sine', 'Amplitude', ...
num2str(app.AmplitudeEditField.Value));
set_param('AppDesignSetup', 'StopTime',num2str(app.DurationEditField.Value));
set_param('AppDesignSetup', 'SimulationMode', 'normal');
set_param('AppDesignSetup', 'SimulationCommand','start');
end
I already shared you the documentation for this. Hence I would request you next time please follows the steps provided in the answer and documentation.
Wish you all the best.

Sign in to comment.

More Answers (0)

Asked:

on 12 Jun 2021

Commented:

on 30 Jun 2021

Community Treasure Hunt

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

Start Hunting!