How to Implement a pause button in appdesigner while frames of video are being displayed in another figure?
Show older comments
I've used this technique in a previous app and had no problems but it is not working in the current one, both made in appdesginer.
One button when pushed is a for loop that does calculations and displayes an image with several notations in a separate figure than the one with the buttons. After the image is plotted the for loop includes the following:
if app.PauseState.Value
waitfor(app.PauseState,'UserData',1);
end
If the user pushes the "Pause" button the PauseStateValueChanged callback is:
if app.PauseState.Value
app.PauseState.UserData = 0;
app.PauseState.Text = 'Resume';
else
app.PauseState.UserData =1;
app.PauseState.Text = 'Pause';
end
drawnow;
In the first app I made, pushing the 'Pause' button causes the for loop to wait to continue plotting until pushing the 'Pause' button again. However the second app the 'Pause' button does nothing until the loop is finished. I looked at teh priority of each button and they say interuptible. Any ideas?
Answers (1)
Voss
on 4 Aug 2023
Put a drawnow in the for loop, before checking app.PauseState.Value, i.e.:
drawnow
if app.PauseState.Value
waitfor(app.PauseState,'UserData',1);
end
(The other drawnow, in PauseStateValueChanged, may be unnecessary.)
4 Comments
Here's a complete piece of code that exhibits the behavior I think you want. You can run it and refer to it to figure out how to apply the same logic to your app (but I think you just need that drawnow in the for loop before checking app.PauseState.Value):
function test_func()
f = uifigure();
b = uibutton(f,'state','Text','Pause','ValueChangedFcn',@cb_btn,'Value',0,'UserData',1);
figure
while true
plot(rand(1,10))
drawnow
if b.Value
waitfor(b,'UserData',1);
end
end
function cb_btn(~,~)
if b.Value
b.UserData = 0;
b.Text = 'Resume';
else
b.UserData = 1;
b.Text = 'Pause';
end
% drawnow
end
end
Taylor
on 4 Aug 2023
Voss
on 4 Aug 2023
You're welcome! Any questions, let me know. Otherwise, please "Accept This Answer". Thanks!
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!