MATLAB Answers

How can I update the plot in App designer?

152 views (last 30 days)
Rasheed Khan
Rasheed Khan on 11 Jun 2020
Commented: Rasheed Khan on 15 Jun 2020
Briefly saying- 1. I am having one UIAxes figure, 2 pushbuttons.
Intially the values in code be like
x=0:0.1:10;
m=1;
y=mx;
curve=animatedline('color','r','Marker','o');
for i=1:length(x)
addpoints(curve,x(i),y(i))
plot(curve,x(i),y(i));
drawnow;
end
One push button is to start plotting.
Another pushbutton is to modify the variable('m') like m+5 and in same figure the plot has to continue with new 'm'.
I am attaching the image that explains what i need clearly.
its not exactly x=50, as soon as I press button to update m it has to start plotting with new m.

  0 Comments

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 11 Jun 2020
Rasheed - i might use a timer to update the plot (or animated line) and then whenever the "Update" push button is called, the updated slope m is used the next time the timer callback fires. For example, the below code creates a two-button GUI with axes which sets up the plot, timer, and provides callbacks for each.
function PlotUpdateExample
% initial parameters for the line
x = 0:0.1:10;
m = 1;
% create the GUI
hFig = figure('Position',[360,500,450,285]);
hStartBtn = uicontrol('Style','pushbutton',...
'CallBack', @StartButtonCallback, ...
'String','Start','Position',[315,220,70,25]);
hUpdateBtn = uicontrol('Style','pushbutton',...
'CallBack', @UpdateButtonCallback, ...
'String','Update','Position',[315,180,70,25]);
hAxes = axes('Units','pixels','Position',[50,60,200,185]);
% create the line
%hLine = animatedline('color','r','Marker','o');
hLine = plot(NaN, NaN, 'color','r','Marker','o');
% update the axes
axis(hAxes, 'equal');
set(hAxes, 'XLim', [min(x) max(x)]);
% create the timer
myTimer = timer('Name','MyTimer', ...
'Period',0.5, ...
'StartDelay',1, ...
'TasksToExecute',inf, ...
'ExecutionMode','fixedSpacing', ...
'TimerFcn', @timerCallback);
% define the callbacks
function StartButtonCallback(hObject, eventdata)
start(myTimer);
end
function UpdateButtonCallback(hObject, eventdata)
m = m + 0.5;
end
function timerCallback(hObject, eventdata)
if index > length(x)
stop(myTimer);
else
%addpoints(hLine, x(index),m * x(index));
set(hLine, 'XData', [get(hLine, 'XData') x(index)], 'YData', [get(hLine, 'YData') m * x(index)]);
index = index + 1;
drawnow;
end
end
end
Note how the callbacks are nested within the parent function so that they all have access to the variables defined within. With some modifications, you should be able to get something similar working for your App Designer GUI.

  1 Comment

Rasheed Khan
Rasheed Khan on 15 Jun 2020
Thanks Geoff Hayes. This helped me a lot.

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!