Attempting to write my data into a single CSV file.

조회 수: 1 (최근 30일)
Kyle Davis
Kyle Davis 2019년 1월 30일
댓글: Kyle Davis 2019년 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.

채택된 답변

Ollie A
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
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.
Kyle Davis
Kyle Davis 2019년 1월 30일
Thank you so much!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Andreas Kvalbein Fjetland
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.
  댓글 수: 1
Kyle Davis
Kyle Davis 2019년 1월 30일
Thank you for your help, I will do next time, sorry if my code wasn't easy to understand

댓글을 달려면 로그인하십시오.

카테고리

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