limit time with tic toc

1 回表示 (過去 30 日間)
elo
elo 2020 年 5 月 2 日
回答済み: Ameer Hamza 2020 年 5 月 5 日
The program will has 5 different levels of difficulties which correspond to a number of seconds the use has to enter an answer. how i do that with tic toc function?
X=floor(10*rand(1));
Y=floor(10*rand(1));
format short
formatSpec='Vad blir: %2f x %4f \n' ;
tic
fprintf(formatSpec,X,Y)
riktigt_svar=X*Y;
Ditt_Svar=input('Svar:');
t=toc
pause(0.5);
if Ditt_Svar == riktigt_svar
msgbox('Right answer')
else
if Ditt_Svar ~= riktigt_svar
errordlg(['Right answer is',num2str(riktigt_svar)],'Error')
end
end
  2 件のコメント
Vimal Rathod
Vimal Rathod 2020 年 5 月 5 日
Could you explain more clearly on how you want to use tic toc function, because your code doesn't show any use of it.
elo
elo 2020 年 5 月 5 日
I want to make the person who using the program, enter an answer on a limit time. EX, first level the answr will be in 20s, second level 15s....
But a do not know how i do that/ or to use tic toc.

サインインしてコメントする。

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 5 月 5 日
input() is a blocking function, and there is no good way to timeout on it in MATLAB. The only alternative is to use inputdlg() and use a timer to close it after a specific number of seconds. See the answer to this question: https://www.mathworks.com/matlabcentral/answers/96229-how-can-i-have-a-dialog-box-or-user-prompt-with-a-time-out-period
Here is a short version of the code on that question
f1 = findall(groot, 'type', 'figure');
t = timer('TimerFcn', {@closeit f1}, 'StartDelay', 3);
start(t);
value = inputdlg('myPrompt', 'myTitle');
value = str2double(value{1});
function closeit(obj, ~, f1)
f2 = findall(0, 'Type', 'figure');
fnew = setdiff(f2, f1);
if ishandle(fnew)
close(fnew);
end
stop(obj)
delete(obj);
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by