Collect data from button response

13 views (last 30 days)
Chelsea
Chelsea on 14 Nov 2014
Commented: Chelsea on 14 Nov 2014
Hi, I'm trying to collect keyboard input, to save to a file (either "s" or "l" key...). Here is the code I have now. I had previously used "keyCode" in place of "KbName" in the while loop, but apparently that doesn't exist? (I have defined block and trial elsewhere). I can't seem to figure out what/where the error is? Thanks!
escapeKey = KbName('ESCAPE');
leftKey = KbName('s');
rightKey = KbName('l');
...
secs0=GetSecs;
respToBeMade = true;
while respToBeMade
[keyIsDown,secs, KbName()] = KbCheck;
if KbName('ESCAPE')
ShowCursor;
sca;
return
elseif KbName('s')
response = 1;
respToBeMade = false;
elseif KbName('l')
response = 0;
respToBeMade = false;
end
end
if(response=='1')
keyResp='s';
elseif(response=='0')
keyResp='l';
end
RT=secs-secs0; % Get reaction time
dataFileName='Data';
dataFile=fopen(dataFileName,'a');
if dataFile == -1
error('Error opening data file!');
end
fprintf(dataFile,'%d,', block);
fprintf(dataFile,'%d,', trial);
fprintf(dataFile,'%d,', keyResp);
fprintf(dataFile,'%d,', RT);
fprintf(dataFile,'\n');
fclose(dataFile);
ShowCursor
ListenChar(0);
  1 Comment
Chelsea
Chelsea on 14 Nov 2014
Hi, thanks!
The error was as follows "An indexing expression on the left side of an assignment must have at least one subscript." This is referencing the following line
[keyIsDown,secs, KbName()] = KbCheck;
I think it's because there is nothing inside KbName? I was surprised it worked for you, I can't run the script as is? Is there any other way I could structure this that might work for me?
Thanks!

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 14 Nov 2014
Edited: Geoff Hayes on 14 Nov 2014
Chelsea - you haven't said what your error is, only that you are trying to collect the keyboard input and that presumably it isn't working. Your code to write the data to file is fine (I can run it and it works well if all variables are initializes) but I did notice that the keyboard input of 's' or 'l' was not being written, only the ASCII equivalent. For example if,
block = 42;
trial = 23;
keyResp = 's';
RT = 400.3;
Then the data written to file as
42,23,115,4.003000e+02,
If you want to see the character 's' instead of 115, then change the line that writes this variable to file from
fprintf(dataFile,'%d,', keyResp);
to
fprintf(dataFile,'%s,', keyResp);
This will ensure that the character/string will be written as
42,23,s,4.003000e+02,
EDIT
In addition to the above comments on the output to the file, if the code is run using the Psychtoolbox the following error appears
An indexing expression on the left side of an assignment must have at least one subscript.
due to the line
[keyIsDown,secs, KbName()] = KbCheck;
The use of the brackets is causing the error, and a further problem will arise due to the third output parameter named KbName which conflicts with a function of the same name.
This line of code should be changed to
[keyIsDown,secs,pressedKeys] = KbCheck;
as pressedKeys is a matrix representing the count that each key is pressed. As we are only interested in the escape, 's', and 'l' keys, then the code that follows will need to be changed to
[keyIsDown,secs, pressedKeys] = KbCheck;
if pressedKeys(escapeKey)
ShowCursor;
sca;
return; % note the change here from returns
elseif pressedKeys(leftKey)
keyResp = 's';
respToBeMade = false;
elseif pressedKeys(rightKey)
keyResp = 'l';
respToBeMade = false;
end
Note that the removal of the response local variable which was used in the subsequent if statement. That code, has been put in to the above so that we initialize keyResp given the pressed key.
Try the above and see what happens!
  1 Comment
Chelsea
Chelsea on 14 Nov 2014
Thank you so much, I truly appreciate your detailed answer! Lifesaver :P

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!