Attempting to write my data into a single CSV file.

3 ビュー (過去 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 Center および File ExchangeTiming 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