creating a coundown alarm everytime a function have been asked

1 view (last 30 days)
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 ?

Answers (1)

dpb
dpb on 28 Feb 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...

Categories

Find more on Introduction to Installation and Licensing 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!