-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_nf_data.m
146 lines (129 loc) · 7.39 KB
/
read_nf_data.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
%% Import data from text file.
% Script for importing data from the following text file:
%
% /Users/localadmin/Dropbox/data_projects/placebo_marta/SubjectResponses_N=20.csv
%
% To extend the code to different selected data or a different text file, generate a function instead of a script.
% Auto-generated by MATLAB on 2016/03/10 17:42:04
%% Initialize variables.
filename = 'SubjectResponses_N=20.csv';
delimiter = ',';
startRow = 3;
%% Read columns of data as strings:
% For more information, see the TEXTSCAN documentation.
formatSpec = '%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%[^\n\r]';
%% Open the text file.
fileID = fopen(filename,'r');
%% Read columns of data according to format string.
% This call is based on the structure of the file used to generate this code. If an error occurs for a different file,
% try regenerating the code from the Import Tool.
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'HeaderLines' ,startRow-1, 'ReturnOnError', false);
%Get the headers (i.e. subject ids)
T = readtable(filename);
subjs = T.Properties.VariableNames;
subjs=subjs(~cellfun('isempty',regexp(subjs,'Subject[0-9]+','match')));
%% Close the text file.
fclose(fileID);
%% Convert the contents of columns containing numeric strings to numbers.
% Replace non-numeric strings with NaN.
raw = repmat({''},length(dataArray{1}),length(dataArray)-1);
for col=1:length(dataArray)-1
raw(1:length(dataArray{col}),col) = dataArray{col};
end
numericData = NaN(size(dataArray{1},1),size(dataArray,2));
for col=[1,2,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48]
% Converts strings in the input cell array to numbers. Replaced non-numeric strings with NaN.
rawData = dataArray{col};
for row=1:size(rawData, 1);
% Create a regular expression to detect and remove non-numeric prefixes and suffixes.
regexstr = '(?<prefix>.*?)(?<numbers>([-]*(\d+[\,]*)+[\.]{0,1}\d*[eEdD]{0,1}[-+]*\d*[i]{0,1})|([-]*(\d+[\,]*)*[\.]{1,1}\d+[eEdD]{0,1}[-+]*\d*[i]{0,1}))(?<suffix>.*)';
try
result = regexp(rawData{row}, regexstr, 'names');
numbers = result.numbers;
% Detected commas in non-thousand locations.
invalidThousandsSeparator = false;
if any(numbers==',');
thousandsRegExp = '^\d+?(\,\d{3})*\.{0,1}\d*$';
if isempty(regexp(thousandsRegExp, ',', 'once'));
numbers = NaN;
invalidThousandsSeparator = true;
end
end
% Convert numeric strings to numbers.
if ~invalidThousandsSeparator;
numbers = textscan(strrep(numbers, ',', ''), '%f');
numericData(row, col) = numbers{1};
raw{row, col} = numbers{1};
end
catch me
end
end
end
%% Split data into numeric and cell columns.
rawNumericColumns = raw(:, [1,2,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48]);
rawCellColumns = raw(:, [3,4]);
%% Replace non-numeric cells with NaN
R = cellfun(@(x) ~isnumeric(x) && ~islogical(x),rawNumericColumns); % Find non-numeric cells
rawNumericColumns(R) = {NaN}; % Replace non-numeric cells
%% Allocate imported array to column variable names
VarName1 = cell2mat(rawNumericColumns(:, 1));
run = cell2mat(rawNumericColumns(:, 2));
stim = rawCellColumns(:, 1);
feedback = rawCellColumns(:, 2);
InfusionNoinfusionRatings = cell2mat(rawNumericColumns(:, 3));
FeedbackRatins = cell2mat(rawNumericColumns(:, 4));
InfusionNoinfusionRatings1 = cell2mat(rawNumericColumns(:, 5));
FeedbackRatins1 = cell2mat(rawNumericColumns(:, 6));
InfusionNoinfusionRatings2 = cell2mat(rawNumericColumns(:, 7));
FeedbackRatins2 = cell2mat(rawNumericColumns(:, 8));
InfusionNoinfusionRatings3 = cell2mat(rawNumericColumns(:, 9));
FeedbackRatins3 = cell2mat(rawNumericColumns(:, 10));
InfusionNoinfusionRatings4 = cell2mat(rawNumericColumns(:, 11));
FeedbackRatins4 = cell2mat(rawNumericColumns(:, 12));
InfusionNoinfusionRatings5 = cell2mat(rawNumericColumns(:, 13));
FeedbackRatins5 = cell2mat(rawNumericColumns(:, 14));
InfusionNoinfusionRatings6 = cell2mat(rawNumericColumns(:, 15));
FeedbackRatins6 = cell2mat(rawNumericColumns(:, 16));
InfusionNoinfusionRatings7 = cell2mat(rawNumericColumns(:, 17));
FeedbackRatins7 = cell2mat(rawNumericColumns(:, 18));
InfusionNoinfusionRatings8 = cell2mat(rawNumericColumns(:, 19));
FeedbackRatins8 = cell2mat(rawNumericColumns(:, 20));
InfusionNoinfusionRatings9 = cell2mat(rawNumericColumns(:, 21));
FeedbackRatins9 = cell2mat(rawNumericColumns(:, 22));
InfusionNoinfusionRatings10 = cell2mat(rawNumericColumns(:, 23));
FeedbackRatins10 = cell2mat(rawNumericColumns(:, 24));
InfusionNoinfusionRatings11 = cell2mat(rawNumericColumns(:, 25));
FeedbackRatins11 = cell2mat(rawNumericColumns(:, 26));
InfusionNoinfusionRatings12 = cell2mat(rawNumericColumns(:, 27));
FeedbackRatins12 = cell2mat(rawNumericColumns(:, 28));
InfusionNoinfusionRatings13 = cell2mat(rawNumericColumns(:, 29));
FeedbackRatins13 = cell2mat(rawNumericColumns(:, 30));
InfusionNoinfusionRatings14 = cell2mat(rawNumericColumns(:, 31));
FeedbackRatins14 = cell2mat(rawNumericColumns(:, 32));
InfusionNoinfusionRatings15 = cell2mat(rawNumericColumns(:, 33));
FeedbackRatins15 = cell2mat(rawNumericColumns(:, 34));
InfusionNoinfusionRatings16 = cell2mat(rawNumericColumns(:, 35));
FeedbackRatins16 = cell2mat(rawNumericColumns(:, 36));
InfusionNoinfusionRatings17 = cell2mat(rawNumericColumns(:, 37));
FeedbackRatins17 = cell2mat(rawNumericColumns(:, 38));
InfusionNoinfusionRatings18 = cell2mat(rawNumericColumns(:, 39));
FeedbackRatins18 = cell2mat(rawNumericColumns(:, 40));
InfusionNoinfusionRatings19 = cell2mat(rawNumericColumns(:, 41));
FeedbackRatins19 = cell2mat(rawNumericColumns(:, 42));
InfusionNoinfusionRatings20 = cell2mat(rawNumericColumns(:, 43));
FeedbackRatins20 = cell2mat(rawNumericColumns(:, 44));
InfusionNoinfusionRatings21 = cell2mat(rawNumericColumns(:, 45));
FeedbackRatins21 = cell2mat(rawNumericColumns(:, 46));
%% concatenate
feedback_ratings = [FeedbackRatins FeedbackRatins1 FeedbackRatins2 FeedbackRatins3 FeedbackRatins4 FeedbackRatins5 ...
FeedbackRatins6 FeedbackRatins7 FeedbackRatins8 FeedbackRatins9 FeedbackRatins10 FeedbackRatins11 ...
FeedbackRatins12 FeedbackRatins13 FeedbackRatins14 FeedbackRatins15 FeedbackRatins16 FeedbackRatins17 ...
FeedbackRatins18 FeedbackRatins19 FeedbackRatins20 FeedbackRatins21];
stim_ratings = [InfusionNoinfusionRatings InfusionNoinfusionRatings1 InfusionNoinfusionRatings2 InfusionNoinfusionRatings3 InfusionNoinfusionRatings4...
InfusionNoinfusionRatings5 InfusionNoinfusionRatings6 InfusionNoinfusionRatings7 InfusionNoinfusionRatings8 ...
InfusionNoinfusionRatings9 InfusionNoinfusionRatings10 InfusionNoinfusionRatings11 InfusionNoinfusionRatings12 ...
InfusionNoinfusionRatings13 InfusionNoinfusionRatings14 InfusionNoinfusionRatings15 InfusionNoinfusionRatings16 ...
InfusionNoinfusionRatings17 InfusionNoinfusionRatings18 InfusionNoinfusionRatings19 InfusionNoinfusionRatings20 ...
InfusionNoinfusionRatings21];
%% Clear temporary variables
clearvars filename delimiter formatSpec fileID dataArray ans raw col numericData rawData row regexstr result numbers invalidThousandsSeparator thousandsRegExp me rawNumericColumns rawCellColumns R T;