Problems when running GUI graph plotting with 'while' or 'if' loops

Hi, I am doing a GUI plotting, trying to implement a real-time plot. I use 'if' loop for different conditions with the pushbutton callback, but when 'if' loop is used, there is error and the other conditions will not execute. So, I use 'while' loop instead and it works. The problem is, when the 'while' loop is used, the command inside the 'while' loop cannot run normally as the conditions keep refreshing by the loop. Can anyone help me?
My function looks like this, inside a pushbutton callback:
if hObject.String=='Start'
set(handles.pushbutton1,'String','Pause');
for i=1:end
plot(x);
end
end
while hObject.String=='Pause' % when 'if' is used here, the plot won't stop plotting
set(handles.pushbutton1,'String','Cont.');
plot(x(1:i));
% at here I want to select and highlight data, since 'while' loop is running, the dragged plot will return to the 'while' loop place and the datatip unable to be selected.
end
while hObject.String=='Cont.'
set(handles.pushbutton1,'String','Pause');
j=i;
for i=j:end
plot(x);
end
end

 채택된 답변

Rik
Rik 2019년 7월 29일

0 개 추천

You should be using strcmp to test if a char array is equal to another char array. Your current tests are array operations.
Also, if is not a loop. The only ways to start a loop is with while, for, and parfor.
A final note: two of your loops just repeat the same code without any dependancy on the loop variable. Why are you using a loop at all there? Unless x is a function, you should use the pause function to stop plotting for a moment.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

Wen
2019년 7월 28일

댓글:

Wen
2019년 7월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by