limit time with tic toc

1 visualización (últimos 30 días)
elo
elo el 2 de Mayo de 2020
Respondida: Ameer Hamza el 5 de Mayo de 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 comentarios
Vimal Rathod
Vimal Rathod el 5 de Mayo de 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 el 5 de Mayo de 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.

Iniciar sesión para comentar.

Respuesta aceptada

Ameer Hamza
Ameer Hamza el 5 de Mayo de 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

Más respuestas (0)

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Etiquetas

Aún no se han introducido etiquetas.

Community Treasure Hunt

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

Start Hunting!

Translated by