Attempting to write my data into a single CSV file.
    2 views (last 30 days)
  
       Show older comments
    
I am currently trying to record both the users keyboard response, and their reaction time associated with making each response to the image presented. I am struggling to identify a way in which I can write and save both the response and reaction time in the same CSV.file. I was wondering if anyone had any suggestions? I currently have the following code;
d=21; %Number of images to load in is 21. 
if d<1 || d>21 %A check to ensure that the correct number of images will be displayed. 
    disp('Error, the number of images loaded in out of range')
end
rts= (zeros(21, 1)); %rts is a numeric array, 21, 1, made up of zeros. 
response=(zeros(21, 1)); % response is a numeric array, 21, 1, made up of zeros. 
dirname= 'C:\Users\User\Documents\MATLAB\stimuli\'; %Location of stimuli  
count= 0;
d=dir([dirname '*.jpg']); 
for a =randperm(numel(d))% return scalar count of elements in matrix
    %And randomly present the images under the variable of d. 
    a= imread(d(a).name); %read in images in desired folder
    imshow(a) %show selected images
    pause(2); %allow a 2 second pause between successive stimuli.
    b= imread('FixationDot.jpg');% read in the fixation dot 
    imshow(b) %show the fixation dot between every letter
    pause(0.5); %allow the fixation dot to remain there for 0.5 seconds
    tic
    count= count+1;
    response(count)= getkey(); %Gain user input 
    rts(count)= toc; %Record the time taken to produce a response from user     
end
M= char(response); %convert ASCII codes to underlying char key press
csvwrite('response.csv', response); %writes user response into a CSV file
csvwrite('reaction_time.csv', rts); %writes users reaction time
Thank you to anyone that can help, I really appreciate it. 
0 Comments
Accepted Answer
  Ollie A
      
 on 30 Jan 2019
        
      Edited: Ollie A
      
 on 30 Jan 2019
  
      I would create a table, using the MATLAB function
T = table(response, reaction_time);
and then simply writing the table to a csv file:
writetable(T,'data.csv')
The benefit of using table() is that you can easily include column headers.
You might also like to include the stimuli image number in another column, i.e.
T = table(imagenumber, response, reaction_time);
2 Comments
  Ollie A
      
 on 30 Jan 2019
				If you want to go further, you can first change the getkey() output from ASCII to a character by doing
response{count} = char(getkey());
Tables can handle data of different formats as well, so it doesn't matter that the first column is a cell array and the second is an array of numbers.
More Answers (1)
  Andreas Kvalbein Fjetland
      
 on 30 Jan 2019
        
      Edited: Andreas Kvalbein Fjetland
      
 on 30 Jan 2019
  
      This should do the trick
response = zeros(21,1);
rts = zeros(21,1);
% Looop
resultTable = table(response,rts);
writetable(resultTable,'fileName.csv')
Please use the code function in the editor next time. Makes your code easier to read and copy.
See Also
Categories
				Find more on Tables in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

