State button callback- break while loop?

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

 Réponse acceptée

Rik
Rik le 3 Sep 2020

0 votes

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 commentaire

Veena Chatti
Veena Chatti le 3 Sep 2020
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?

Connectez-vous pour commenter.

Plus de réponses (1)

Mohammad Sami
Mohammad Sami le 3 Sep 2020
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 commentaires

Veena Chatti
Veena Chatti le 3 Sep 2020
Thanks, I tried this, but it still goes through the whole while loop, and doesn't stop in between even if the button is pushed again.
Rik
Rik le 3 Sep 2020
I don't have much experience with AppDesigner, but I would have expected the code from Mohammad to work. You might need drawnow in your while loop to force Matlab to execute queued callbacks.
Mohammad Sami
Mohammad Sami le 3 Sep 2020
Modifié(e) : Mohammad Sami le 3 Sep 2020
Are you using a state button ? The code is written for use with state button.
If that does not work, you can use drawnow as suggested by Rik.
Thanks, Mohammad and Rik! Yes, I am using a state button.
Setting a flag under the control of the button and using drawnow worked well. I also think your code would have worked, Mohammad, with drawnow.
Here's the functional code that I decided to use, in case anyone trying to do something similar comes across this thread:
value = app.pswitch.Value;
j = get(app.Spinner, 'Value');
set(app.pswitch, 'Text', 'Pause');
flag = 1; % Set flag
while(flag) && j < 299
pause(1/1000)
j = get(app.Spinner, 'Value');
set(app.Spinner, 'Value', j+1);
set(app.Slider, 'Value', j+1);
app.I = imshow(app.testrgb(:,:,3,j+1), [300 1300],...
'Parent', app.UIAxes); % Display command
drawnow; % get it to check
f = get(app.pswitch,'Value');
if isequal(f, 0)
set(app.pswitch, 'Text', 'Play');
flag = 0;
break;
elseif ~isequal(f,1)
set(app.pswitch, 'Text', 'Pause');
end
end
This is a bit off topic, but do either of you (or anyone else who sees this) know the smallest time interval and resolution it's possible to set with pause()?
Rik
Rik le 3 Sep 2020
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 le 12 Sep 2020
Thank you! I figured it out.

Connectez-vous pour commenter.

Catégories

En savoir plus sur App Building dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by