GUI ButtonPushed event: how to use a while loop and tic/toc to set time to 5sec?

16 次查看(过去 30 天)
I am making a GUI using App Designer and need help with a ButtonPushed function. In the while loop, I need to set the run time to 5sec and also make the value of a Gauge proportional to the time elapsed, so that the gauge will advance as time goes on and have a final value of 100 when 5sec is reached
would the while loop conditon be ...
tic
while toc==5
...
end
How would i make the gauge value proportional to the time elapsed?
Thanks in advance for any suggestions

回答(1 个)

Tarunbir Gambhir
Tarunbir Gambhir 2020-10-30
You can use the following inside the callback function that starts the timer for 5 seconds
app.Gauge.Value = 0;
tic;
while toc<=5
app.Gauge.Value = toc/5*100;
pause(1/1000);
end
app.Gauge.Value = 100;
The while loop will run till "toc" returns value less than 5(seconds). In every iteration of the loop the Gauge value will be updated to a value between 0 and 100 proportional to time elapsed out of 5 seconds.
The "pause" is used so that some time is given for the Gauge display to update the value, the pause duration (in seconds) can be reduced as required to increase the smoothness in Guage motion.
At the end of the loop, the gauge value will be very close to 100 (of the order of the pause duration). Which is why the Gauge value is set to 100 (max) right after the loop ends.

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by