Attempting to write my data into a single CSV file.
조회 수: 1 (최근 30일)
이전 댓글 표시
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
채택된 답변
Ollie A
2019년 1월 30일
편집: Ollie A
2019년 1월 30일
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
Ollie A
2019년 1월 30일
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.
추가 답변 (1개)
Andreas Kvalbein Fjetland
2019년 1월 30일
편집: Andreas Kvalbein Fjetland
2019년 1월 30일
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.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!