forked from LIMO-EEG-Toolbox/limo_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
limo_combine_catvalues.m
74 lines (65 loc) · 2.31 KB
/
limo_combine_catvalues.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
function [single_col,final_matrix] = limo_combine_catvalues(varargin)
% simple routine to combine columns from a categorical variable file
%
% FORMAT out = limo_combine_catvalue(cat_file, options)
%
% INPUT cat_file is simply the file name of your text file that include
% multiple columns coding each for levels in a factor
% option: 'columns' followed by the columns you want to combine
% (default is all)
% 'format' is 'txt' to create a new text file or 'var' for
% simply returning the new combined values (default)
%
% OUTPUT single_col is either the name of the new text file created or the new
% combined values (see optons)
% final_matrix is a matrix form of 1 and 0
%
% EXEMPLES
% new_file = limo_combine_catvalues('./mydocs/myfile.txt', 'columns', [1 2 3],'format', 'txt'))
% new_values = limo_combine_catvalues('./mydocs/myfile.txt', 'format', 'var')
%
% Cyril Pernet 27 June 2016
% ------------------------------------------
% Copyright (C) LIMO Team 2016
%% Check data input
% defaults
columns = 'all';
format = 'val';
if nargin == 0
[filename, pathname] = uigetfile('*.txt', 'Pick a text file for your categorical variables');
if isequal(filename,0) || isequal(pathname,0)
disp('User pressed cancel'); return
else
disp(['User selected ', fullfile(pathname, filename)])
varargin{1} = fullfile(pathname, filename);
end
cat_values = load(filename);
elseif nargin == 1
cat_values = load(varargin{1});
else
cat_values = load(varargin{1});
for n=1:(nargin-1)
if strcmpi(varargin{n},'columns')
columns = varargin{n+1};
elseif strcmpi(varargin{n},'format')
format = varargin{n+1};
end
end
end
if ischar(columns)
combine = [1:size(cat_values,2)];
else
combine = columns;
end
%% Combine specified columns
[x,nb_conditions]= limo_quick_design(cat_values(:,combine));
[tmpX interactions] = limo_make_interactions(x, nb_conditions);
N = size(tmpX,2) - interactions(end) + 1;
final_matrix = tmpX(:,N:end);
%% output
single_col = sum((repmat([1:interactions(end)],[size(final_matrix,1),1]) .* final_matrix),2);
if strcmpi(format,'txt')
name = [varargin{1}(1:end-4) '_combined.txt'];
save(name,'single_col','-ascii');
single_col = name;
end