State button callback- break while loop?

조회 수: 17 (최근 30일)
Veena Chatti
Veena Chatti 2020년 9월 3일
댓글: Veena Chatti 2020년 9월 12일
Hi,
I'm trying to loop through hundreds of frames in a file with the push of a state button and the use of a slider in AppDesigner. With the code below, what I've managed so far is to get it to loop until the end of all frames and then stop.
What I'd like it to do further, however, is to loop continuously until the button is pressed, and stop looping by breaking the while loop when the button is un-pressed.
I would greatly appreciate all your tips and suggestions!
Thanks!
v = app.pswitch.Value;
switch v
case 0
% do nothing
case 1
j = get(app.Slider, 'Value');
while j < 300 % (number of frames)
if v == 1
% do this:
pause(1/1000)
j = get(app.Slider, 'Value');
app.Spinner.Value = j+1; % go to the next frame
app.Slider.Value = j+1; % go to the next frame
app.I = imshow(app.testrgb(:,:,3,j+1), [300 1300],...
'Parent', app.UIAxes); % display the next frame
end
end
app.Spinner.Value = 1;
app.Slider.Value = 1;
set(app.pswitch, 'Value', 0)
otherwise
% do nothing
end

채택된 답변

Rik
Rik 2020년 9월 3일
In general the solution to this is to use a flag that is controlled by your button. Then inside the while loop you check for that flag (a simple if flag,break,end will do).
  댓글 수: 1
Veena Chatti
Veena Chatti 2020년 9월 3일
Thanks, I'd like to try this. Where can I find some tips about how to use a flag and have it under the control of the button?

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

추가 답변 (1개)

Mohammad Sami
Mohammad Sami 2020년 9월 3일
You can try something like this.
app.STOPButton.Enable = true;
app.STOPButton.Value = 0;
while(somecondition)
% do something
if(app.STOPButton.Value == 1)
break;
end
end
app.STOPButton.Enable = false;
app.STOPButton.Value = 0;
  댓글 수: 6
Rik
Rik 2020년 9월 3일
The theoretical limit would be eps. As a practical limit I would suggest 30ms.
It isn't hard to test this, just use a loop to make the time smaller until tic,toc is telling you the pause is much longer than the time you asked for. I you want I can write some code for you. I'm currently on mobile so writing an example is more challenging.
Veena Chatti
Veena Chatti 2020년 9월 12일
Thank you! I figured it out.

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by