creating a coundown alarm everytime a function have been asked

1 次查看(过去 30 天)
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 ?

回答(1 个)

dpb
dpb 2021-2-28
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...

类别

Help CenterFile Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by