Close GUI menu after discrete time period or user keystroke
Mostrar comentarios más antiguos
I'm making an n-back test using MATLAB 7.10.0, and I need to acquire either or both keystrokes 'A' and 'S' from the user within 2.5s. If 2.5s passes without any keystrokes pressed, then the program will set the response to 0.
Simple algorithm: 1) Ask user for data 2) Start timer, t = 0s 3) Acquire keystrokes from the user and store ASCII equivalent as answer. 4) End timer, t = 2.5s 5) If no keystrokes, then answer = 0.
I've looked online and through the Product Documentation, but nothing is coming up -- although I'm sure it's possible.
Thanks in advance.
EDIT: In the same vein, rather than keystrokes as the input, I'd actually like to use the menu function, with three buttons: 'A', 'S', and 'A+S'. But the problem is closing the menu after a discrete time period.
WHAT I REALLY NEED TO KNOW: 1) Time the input period 2) Acquire specific keystroke 3) Close the menu using code -- 'close' fn doesn't work with menu.
Respuestas (2)
Oleg Komarov
el 22 de Ag. de 2011
EDITED
% Create timer object that stops after 2.5 seconds
f = 'h = findobj(0,''Name'',''GETKEY''); if h; uiresume(h); end';
t = timer('StartDelay',2.5,'TasksToExecute',1,'TimerFcn',f);
% Accepted keys are 'A'or 'a' for rist position and 'S' or 's' for second position
keys = false(1,2);
vec = {'A' 'S'};
% Start timer
start(t)
% Run till timer executes
while strcmp(get(t,'Running'),'on')
% Acquire keystroke
key = getkey;
% Check if already pressed
keys = keys | strcmpi(key,vec);
% If both pressed before the timer stops exit the loop
if all(keys)
break
end
end
disp(keys)
14 comentarios
Robert
el 22 de Ag. de 2011
Robert
el 22 de Ag. de 2011
Robert
el 22 de Ag. de 2011
Walter Roberson
el 22 de Ag. de 2011
ismembc() is an internal routine used by ismember(); some people prefer to code ismembc() because it is faster, whereas unless there is strong reason I prefer to code ismember() for clarity.
I think Oleg has miscoded the call: getkey() is going to be a single value, but ismembc() is going to return a logical array as long as its first argument, 4, which is not going to work when or'd (|) against the array false(1,2) used to initialize keys.
Oleg Komarov
el 22 de Ag. de 2011
Yeah, I just noticed when writing that it could be case insensitive and modified inappropriately. Now it should be fine for the dimension mismatch. Haven't looked yet into getkey issue.
Walter Roberson
el 22 de Ag. de 2011
Oleg, did you perhaps want strcmpi(key, {'a', 's'}) instead of 'a' and 'b' ?
Oleg Komarov
el 22 de Ag. de 2011
@Walter: Thanks, corrected.
@Robert: now my edited script takes care of closing getkey (no modification needed).
Robert
el 23 de Ag. de 2011
Robert
el 23 de Ag. de 2011
Robert
el 23 de Ag. de 2011
Oleg Komarov
el 23 de Ag. de 2011
I'll try to test it within a function foo and come back to you.
Oleg Komarov
el 23 de Ag. de 2011
I erased stop(t) from f.
Robert
el 23 de Ag. de 2011
Oleg Komarov
el 23 de Ag. de 2011
If yout hink my answer solved your problem please accept it.
Robert
el 22 de Ag. de 2011
6 comentarios
Robert
el 22 de Ag. de 2011
Walter Roberson
el 22 de Ag. de 2011
What do you do with vec() ?
You get values and assign them in to ch, but as long as the timer is running, you risk overwriting ch with the next value read.
Oleg Komarov
el 22 de Ag. de 2011
Recheck my script, I have modified it to close getkey if nothing happens in x seconds and fixed the other error.
It is case insensitive, wether you press 'A' or 'a'. But you can use strcmp instead of strcmpi to make it case sensitive.
Robert
el 22 de Ag. de 2011
Oleg Komarov
el 22 de Ag. de 2011
Fixing one last thing, if you have typed both A and S it exits the loop and when the timer stops later it calls uiresume but at that point no getkey figure exists thus it creates and empty one.
Oleg Komarov
el 22 de Ag. de 2011
Ok now it should work properly.
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!