creating a coundown alarm everytime a function have been asked

조회 수: 1 (최근 30일)
Mouhamad Ibrahim
Mouhamad Ibrahim 2021년 2월 28일
답변: dpb 2021년 2월 28일
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