Saving responses to a matrix

3 次查看(过去 30 天)
Katerina Tetzloff
Katerina Tetzloff 2015-11-23
编辑: dpb 2015-11-26
I am using PsychToolbox to make an experiment. Right now I have stimuli being presented (in a random order each time), and then the participant presses a response on the keyboard. I have it so that the stimulus number and the response key pressed save to a .txt file, but is there any way to save these to a matrix?? I want to be able to sort the stimuli and responses so that they can be in numerical order at the end of the experiment in order to save myself a lot of time in organizing the data after the fact. I tried saving the responses to variables and doing this, as below, but it didn't work.
stim = number stimulus that is shown
res = response key pressed (I have these two things working)
mat = [];
CODE RUNS LOOP, PRESENTS ONE STIMULUS AND RECORDS RESPONSE;
mat = [mat, stim, res];
But it gives me an error because my matrix isn't balanced. I basically want it to save the stim and responses in an organized form in a matrix e.g. [stim, res; stim, res; etc.] so that I can easily sort it after. Any suggestions??? THANKS!!!!

回答(1 个)

dpb
dpb 2015-11-24
What variable type is the response; numeric or character?
If it's numeric you can simply write
mat=[mat;[sim res]];
appending a new vector on the end.
If it's character it needs must be a cell array to hold the two types together in a single array or, you might look at the data table object as a composite holding type that may be convenient for such data.
While it's true that dynamic reallocation is not recommended, for an interactive code such as this that is controlled in response time by the user interaction and won't have a terribly high number of elements, the cost of not having preallocated the array will be minimal and unnoticed amongst the rest of the interactive delay.
  2 个评论
Katerina Tetzloff
Katerina Tetzloff 2015-11-24
Yes, the responses are numeric.
I tried the code above and got this error:
Error using vertcat Dimensions of matrices being concatenated are not consistent.
Thanks!!
dpb
dpb 2015-11-24
编辑:dpb 2015-11-26
Well, it ain't what you showed/say it is then... _
>> mat=[];
>> mat=[mat;2 pi]
mat =
2.0000 3.1416
>> mat=[mat;[2 pi]]
mat =
2.0000 3.1416
2.0000 3.1416
>>
works just fine as shown either with/without the explicit brackets for the two elements being added...
Of course, if one is a vector or has differing number of elements on subsequent call, then
>> mat=[mat;2 pi 3]
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
>>
In that case you'd have to use a cell array to hold the varying-length response or fix the code to prevent having such a problem if that's not what is expected.
What's
whos sim res
show from the command line?
ADDENDUM
OBTW, it also is a problem if the added row is short, not long...
>> mat=[mat; pi]
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
>>
Same error message; same error just the alternate difficulty of not having as many elements instead of too many...

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Timing and presenting 2D and 3D stimuli 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by