how to set answering time window that awaits specific keypresses as response in MATLAB 2011b?

1 vue (au cours des 30 derniers jours)
I am trying to write this task that presents an image and awaits the participant to answer by pressing eighter left arrow key or right arrow key. answering time is limited and if the time passes the next image will be presented. I am using MATLAB 2011b for some reasons.
pseudo code would be like:
function = pushbutton_callback()
if leftArrow
result = left;
else
result = right
end
set(gcf, 'keypressFcn', @pushbutton_callback)
while trials < 60
presentImage();
if keypressed
continue
elseif timer > 2 & keypressed = false
result = null
continue
end
end

Réponse acceptée

Jan
Jan le 30 Mai 2021
Modifié(e) : Jan le 30 Mai 2021
% [TESTED in R2009a]
function test
FigH = figure('KeyPressFcn', @KeyPressed);
TimerH = timer('TimerFcn', @(TimerH, EventData) uiresume(FigH), ...
'ExecutionMode', 'SingleShot');
for k = 1:60
% presentImage();
set(FigH, 'UserData', 0); % Set default key
% Wait for 2 seconds or until key was pressed:
startat(TimerH, clock + [0,0,0,0,0,2]); % Overflow caught smartly
uiwait(FigH);
stop(TimerH); % Ignore further timeout
if ~ishandle(FigH) % catch deleted figure:
disp('Figure was deleted.');
break;
end
% Check if a key was pressed:
UD = get(FigH, 'UserData');
switch double(UD)
case 28
disp('left arrow')
case 29
disp('right arrow')
case 0
disp('Too slow');
end
end
end
function KeyPressed(FigH, EventData)
C = get(FigH, 'CurrentCharacter');
set(FigH, 'UserData', C);
uiresume(FigH);
end
With modern Matlab versions, use in KeyPressed():
c = EventData.Key;
and
set(FigH, 'UserData', 'noKey');
...
UD = get(FigH, 'UserData');
switch UD
case 'leftarrow'
...
case 'rightarrow'
...
case 'noKey'
end

Plus de réponses (0)

Catégories

En savoir plus sur Graphics Performance dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by