How do I keep the target for a specific time period?

4 views (last 30 days)
I am trying to let my target (*) stay for .5 sec if the key is not pressed by the user and then continue to another trial. If the key is pressed then I want to record the reaction time. The code I have put in doesn't seem to be working.
if input == get(gcf, 'CurrentCharacter')
tic
toc = RT
elseif isempty(input)
pause(.5)
end
This is the full script.
%Lab Assignment 2: Posner Task
ntrials = 20 %number of trials
RT = zeros(2,ntrials) %reaction time for 1 row valid and 2 invalid
%create a matrix for recording valid and invalid trials
Valid = [1]
invalid = [Valid-1]
%Create the figure for trial
t = figure
xlim([1 5])
ylim([1 5])
lower_l=rectangle('Position', [1.3 2.8 .5 .5]);
hold on
upper_l=rectangle('Position', [2.3 2.8 .5 .5]);
hold on
lower_r=rectangle('Position', [3.3 2.8 .5 .5]);
hold on
upper_r=rectangle('Position', [4.3 2.8 .5 .5]);
hold on
cross=text(3, 3, '+');
set(cross, 'fontsize', 20);
%create conditions for trails
for ntrials = 1:20
rnd = rand; %randomizing the cue for trials
if (rnd < .25)
set(lower_r, 'EdgeColor', 'r')
pos = 3
pause(0.1)
set(lower_r, 'EdgeColor', 'k')
pause(0.3)
elseif (rnd > .25) && (rnd < .5)
set(lower_l, 'EdgeColor', 'r')
pos = 1
pause(0.1)
set(lower_l, 'EdgeColor', 'k')
pause(0.3)
elseif (rnd > .5) && (rnd < .75)
set(upper_l, 'EdgeColor', 'r')
pos = 2
pause(0.1)
set(upper_l, 'EdgeColor', 'k')
pause(0.3)
else
set(upper_r, 'EdgeColor', 'r')
pos = 4
pause(0.1)
set(upper_r, 'EdgeColor', 'k')
pause(0.3)
end
target_rand = rand;
if (target_rand > .85) %no target condition
elseif (target_rand < .425) %valid trial conditions
if pos == 4
Targ3 = plot(4.5, 3, '*') %plot target at 1
pause (.15)
set(Targ3, 'Marker', 'none')
elseif pos == 1
Targ1 = plot(1.5, 3, '*') %target at 2
pause (.15)
set(Targ1, 'Marker', 'none')
elseif pos == 2
Targ2 = plot(2.5, 3, '*') % target at 3
pause (.15)
set(Targ2, 'Marker', 'none')
else pos == 3
Targ4 = plot(3.5, 3, '*')
pause (.15)
set(Targ4, 'Marker', 'none')
end
elseif pos == 4 %invalid trial conditions
rand_invalid = rand;
if (rand_invalid > .33)
Targ1 = plot(1.5, 3, '*') % target at 2
pause (.15)
set(Targ1, 'Marker', 'none')
elseif (rand_invalid < .33)
Targ2 = plot(2.5, 3, '*') %target at 3
pause (.15)
set(Targ2, 'Marker', 'none')
else
Targ4 = plot(3.5, 3, '*') %target at4
pause (.15)
set(Targ4, 'Marker', 'none')
end
elseif pos == 1
rand_invalid = rand;
if (rand_invalid > .33)
Targ3 = plot(4.5, 3, '*') %targ at 1
pause (.15)
set(Targ3, 'Marker', 'None')
elseif (rand_invalid < .33)
Targ2 = plot(2.5, 3, '*') %targ at 3
pause (.15)
set(Targ2, 'Marker', 'none')
else
Targ4 = plot(3.5, 3, '*') %targ at 4
pause (.15)
set(Targ4, 'Marker', 'none')
end
elseif pos == 2
rand_invalid = rand;
if (rand_invalid > .33)
Targ3= plot(4.5, 3, '*') %targ at 1
pause (.15)
set(Targ3, 'Marker', 'none')
elseif (rand_invalid < .33)
Targ1 = plot(1.5, 3, '*')%targ at 2
pause (.15)
set(Targ1, 'Marker', 'none')
else
Targ4 = plot(3.5, 3, '*') %targ at 4
pause (.15)
set(Targ4, 'Marker', 'none')
end
elseif pos == 3
rand_invalid = rand;
if (rand_invalid > .33)
Targ3 = plot(4.5, 3, '*') %targ at 1
pause (.15)
set(Targ3, 'Marker', 'none')
elseif (rand_invalid < .33)
Targ1 = plot(1.5, 3, '*') %targ at 2
pause (.15)
set(Targ1, 'Marker', 'none')
else
Targ2 = plot(2.5, 3, '*') %targ at 3
pause (.15)
set(Targ2, 'Marker', 'none')
end
%Press key if target is present and record RT. If not pressed
%then go to the next trial after a pasue
if input == get(gcf, 'CurrentCharacter')
tic
toc = RT
elseif isempty(input)
pause(.5)
end
%mean RT for valid and invalid tials
%RT_Valid) = mean(RT(Valid))
%RT_invalid = mean(RT(invalid))
end
%add pause between each trial
pause(1)
end
I am new to MATLAB, any help will be appreciated!

Answers (2)

Deepak
Deepak on 10 Dec 2024
To record reaction times when a key is pressed and ensure a pause when no key is pressed, we can use “waitforbuttonpress” function in MATLAB.
Start timing with “tic” when the target appears. If a key is pressed, capture it with “get(gcf, 'CurrentCharacter')” and compute the reaction time using “toc”, storing it in the “RT” matrix.
If no key is pressed, simply “pause” for 0.5 seconds before proceeding to the next trial. This method ensures accurate reaction time recording and smooth trial transitions.
Below is the specific code changes required in your code:
% Inside the main loop where you display the target
tic; % Start timing
% Wait for a key press or timeout
keyPressed = waitforbuttonpress;
if keyPressed
% A key was pressed
key = get(gcf, 'CurrentCharacter');
reactionTime = toc; % Stop timing and get reaction time
% Store the reaction time in the RT matrix
% Use pos to determine valid or invalid trial
if target_rand < .425
RT(1, ntrials) = reactionTime; % Valid trial
else
RT(2, ntrials) = reactionTime; % Invalid trial
end
else
% No key was pressed, wait for .5 seconds
pause(0.5);
end
Please find attached the documentation of functions used for reference:
I hope this assists in resolving the issue.

Walter Roberson
Walter Roberson on 10 Dec 2024
You need a different approach.
You do not want to pause(0.5) -- you want to wait up to 0.5 for a key press and stop waiting as soon as the button is pressed
If you use pure MATLAB, you need to code it as a "busy loop", along the lines of
fig = gcf;
fig.CurrentCharacter = '';
tic;
reaction_time = nan;
while toc < 0.5
key = fig.CurrentCharacter;
if ~isempty(key)
reaction_time = toc;
break;
end
end
reaction_times = reaction_time;
but better is to use facilities such as those provided by Psychtoolbox, https://www.mathworks.com/matlabcentral/answers/143088-real-time-detect-keypress#answer_285124

Community Treasure Hunt

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

Start Hunting!