creating a coundown alarm everytime a function have been asked

1 vue (au cours des 30 derniers jours)
Mouhamad Ibrahim
Mouhamad Ibrahim le 28 Fév 2021
Réponse apportée : dpb le 28 Fév 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 ?

Réponses (1)

dpb
dpb le 28 Fév 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...

Catégories

En savoir plus sur Introduction to Installation and Licensing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by