/
extract_two_way_data.m
116 lines (101 loc) · 3.47 KB
/
extract_two_way_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
function [od,jd] = extract_two_way_data(varargin)
% Parse inputs
p = inputParser;
addOptional(p,'excel_file_string','');
addOptional(p,'excel_sheet','Sheet 1');
addOptional(p,'data_structure',[]);
addOptional(p,'table',[]);
addOptional(p,'parameter_string','');
addOptional(p,'factor_1','');
addOptional(p,'factor_1_strings','');
addOptional(p,'factor_2','');
addOptional(p,'factor_2_num2str_format',[]);
addOptional(p,'factor_2_strings','');
addOptional(p,'conditions',[]);
addOptional(p,'grouping_string','');
addOptional(p,'convert_grouping_numbers_to_strings',0);
addOptional(p,'exclude_NaNs',1);
parse(p,varargin{:});
p.Results
% Code
% Load input data
if (isempty(p.Results.data_structure))
if (isempty(p.Results.table))
d = table2struct(readtable(p.Results.excel_file_string, ...
'Sheet',p.Results.excel_sheet), ...
'ToScalar',1);
else
d = table2struct(p.Results.table,'ToScalar',1);
end
else
d = p.Results.data_structure;
end
% Convert factor 2 to strings if required
if (~isempty(p.Results.factor_2_num2str_format))
d.(p.Results.factor_2) = ...
cellstr(num2str(d.(p.Results.factor_2),p.Results.factor_2_num2str_format));
end
% Reformat grouping numbers as strings if required
if (p.Results.convert_grouping_numbers_to_strings)
if (isnumeric(d.(p.Results.grouping_string)))
d.(p.Results.grouping_string) = ...
cellstr(num2str(d.(p.Results.grouping_string)));
end
end
% Deduce factor_1_strings
if (isempty(p.Results.factor_1_strings))
factor_1_strings = unique(d.(p.Results.factor_1));
else
factor_1_strings = p.Results.factor_1_strings;
end
% Deduce factor_2_strings
if (isempty(p.Results.factor_2_strings))
factor_2_strings = unique(d.(p.Results.factor_2))
else
factor_2_strings = p.Results.factor_2_strings;
end
% Now organize the data
counter = 0;
for i=1:numel(factor_1_strings)
for j=1:numel(factor_2_strings)
vi = find( ...
strcmp(d.(p.Results.factor_1),factor_1_strings{i}) & ...
strcmp(d.(p.Results.factor_2),factor_2_strings{j}));
if (~isempty(p.Results.conditions))
vi = intersect(vi, ...
find(strcmp(d.(p.Results.conditions{1}), ...
p.Results.conditions{2})));
end
% Exclude NaNs if required
if (p.Results.exclude_NaNs)
y_temp = d.(p.Results.parameter_string)(vi);
vi = vi(find(~isnan(y_temp)));
end
for k=1:numel(vi)
counter=counter+1;
od.(p.Results.factor_1){counter}=factor_1_strings{i};
od.(p.Results.factor_2){counter}=factor_2_strings{j};
if (~isempty(p.Results.grouping_string))
od.(p.Results.grouping_string){counter} = ...
d.(p.Results.grouping_string){vi(k)};
end
if (~isempty(p.Results.conditions))
od.(p.Results.conditions{1}){counter} = ...
d.(p.Results.conditions{1}){vi(k)};
end
od.(p.Results.parameter_string)(counter) = ...
d.(p.Results.parameter_string)(vi(k));
end
if (numel(vi)>0)
jd(i).points{j} = d.(p.Results.parameter_string)(vi);
end
end
end
jd(1).f1_strings = factor_1_strings;
jd(1).f2_strings = factor_2_strings;
% Convert od to a table
field_names = fieldnames(od);
for i=1:numel(field_names)
od.(field_names{i}) = od.(field_names{i})';
end
od = struct2table(od);