limit time with tic toc

1 Ansicht (letzte 30 Tage)
elo
elo am 2 Mai 2020
Beantwortet: Ameer Hamza am 5 Mai 2020
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 Kommentare
Vimal Rathod
Vimal Rathod am 5 Mai 2020
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 am 5 Mai 2020
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Ameer Hamza
Ameer Hamza am 5 Mai 2020
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

Weitere Antworten (0)

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Tags

Noch keine Tags eingegeben.

Community Treasure Hunt

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

Start Hunting!

Translated by