creating a coundown alarm everytime a function have been asked

3 visualizaciones (últimos 30 días)
Mouhamad Ibrahim
Mouhamad Ibrahim el 28 de Feb. de 2021
Respondida: dpb el 28 de Feb. de 2021
Hello, basically what I want to do is that i want to create a simple function like this:
function [] = alarm()
print("5 seconds to wake up")
end
and everytime i call the function without giving it any initial parameter i want the countdown to decrement so the first time i call it: alarm() return: 5 seconds to wake up
second time: alarm() return 4 seconds to wake up
etc... to reach 0
how can i do that ?

Respuestas (1)

dpb
dpb el 28 de Feb. de 2021
function [] = alarm(varargin)
% alarm -- display countdown left
% Optional arguments
% 'init' -- reset counter
persistent SECS
% handle optional arguments, then quit
option=[varargin{:}];
if strncmpi(option,'init',3), SECS=5; return, end
disp(compose("%d seconds to wake up",SECS))
SECS=SECS-1;
end
Above doesn't have niceties of additional options to reset to a new/different time nor does it handle underllow gracefully...but the general idea. Altho I fail to see how can be of any real value as described...

Categorías

Más información sobre Introduction to Installation and Licensing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by