Filter löschen
Filter löschen

Saving responses to a matrix

3 Ansichten (letzte 30 Tage)
Katerina Tetzloff
Katerina Tetzloff am 23 Nov. 2015
Bearbeitet: dpb am 26 Nov. 2015
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!!!!

Antworten (1)

dpb
dpb am 24 Nov. 2015
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 Kommentare
Katerina Tetzloff
Katerina Tetzloff am 24 Nov. 2015
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 am 24 Nov. 2015
Bearbeitet: dpb am 26 Nov. 2015
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...

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by