How can I update the plot in App designer?

98 vues (au cours des 30 derniers jours)
Rasheed Khan
Rasheed Khan le 11 Juin 2020
Commenté : Rasheed Khan le 15 Juin 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.

Réponse acceptée

Geoff Hayes
Geoff Hayes le 11 Juin 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 commentaire
Rasheed Khan
Rasheed Khan le 15 Juin 2020
Thanks Geoff Hayes. This helped me a lot.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Interactive Control and Callbacks dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by