How can I update the plot in App designer?

조회 수: 96 (최근 30일)
Rasheed Khan
Rasheed Khan 2020년 6월 11일
댓글: Rasheed Khan 2020년 6월 15일
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.

채택된 답변

Geoff Hayes
Geoff Hayes 2020년 6월 11일
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
Rasheed Khan
Rasheed Khan 2020년 6월 15일
Thanks Geoff Hayes. This helped me a lot.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by