-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjpa_getCovarsSjinfo.m
More file actions
144 lines (134 loc) · 4.33 KB
/
jpa_getCovarsSjinfo.m
File metadata and controls
144 lines (134 loc) · 4.33 KB
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
function [ids, grp, covarsc, covNotEmpty] = jpa_getCovarsSjinfo(sjinfo, covarNames, varargin)
% Function that loads substructures from sjinfo-Struct,
% returning the ID-Vector, the Group-Vector, all loaded covariates and a
% logical vector showing which covariate was loaded. if function is called
% with two arguments it will search for the ids and groups with the default
% values.
%
% Syntax:
% jpa_getCovarsSjinfo(sjinfo, covarNames[, subStructToSTID, subStructToGrps])
%
% Inputs:
% sjinfo - sjinfo-Struct
% covarNames - names of covariates to load
% substructToIDs - "path" to ID-vector in sjinfo
% substructToGrps - "path" to grp-vector in sjinfo
%
% Outputs:
% ids - ID-string-vector, containing the IDs of all Subjects
% grp - Grp-string-vector, containing the GrpName of all Subjects
% covarsc - NxM-string-vector, containtng all loaded covariates
% covNotEmpty - logical-vector, showing which Covariate could be
% loaded
%
% Example:
% jpa_getCovarsSjinfo(sjinfo, {'cov1 ''cov2'})
% subStructToSTID is default: sjinfo.KAP.STID
% subStructToGrps is default: sjinfo.KAP.PK
% -> output: {'1' '2' '3'}, {'Grp1' 'Grp2' 'Grp2'},
% {[value1cov1, value2cov1, ...],
% [value1cov2, value2cov2, ...]}
%
% jpa_getCovarsSjinfo(sjinfo, {'cov1 ''cov2'}, 'A.IDs','B.Grps')
% -> output: {'1' '2' '3'}, {'Grp1' 'Grp2' 'Grp2'},
% {[value1cov1, value2cov1, ...],
% [value1cov2, value2cov2, ...]}
%
% Other m-files required: jpa_getSubstruct
% Subfunctions: none
% MAT-files required: none
%
% See also: jpa_getSubstruct
% Author: Jan Albrecht
% Work address:
% email: jan-philipp.albrecht@charite.de, j.p.albrecht@fu-berlin.de
% Website:
% Sep 2015; Last revision: 29-Sep-2015
%------------- BEGIN CODE --------------
%% Check for Input Arguments
if nargin < 2
error('Wrong number of arguments. Could not get Covariates')
elseif nargin == 2
subStructToSTID = '';
subStructToGrps = '';
elseif nargin == 3
subStructToSTID = varargin{1};
subStructToGrps = '';
elseif nargin == 4
subStructToSTID = varargin{1};
subStructToGrps = varargin{2};
end
if ~isstruct(sjinfo)
error('Structure of sjinfo is wrong');
end
%% Initialize
numbOfCov = length(covarNames);
covarsc = cell(1,numbOfCov);
covNotEmpty = false(numbOfCov,1);
%% get STID and Grp
try
% get STID
if strcmp(subStructToSTID , '') % do recursive search
ids = jpa_getSubstruct(sjinfo, 'STID');
if isempty(ids); error('Could not find STID!');end
else % try to read given subStruct
ids = sjinfo;
subStructToSTID = textscan(subStructToSTID,'%s','delimiter','.');
subStructToSTID = subStructToSTID{1,1};
for i=1:1:length(subStructToSTID)
ids = ids.(subStructToSTID{i});
end
end
% get PK
if strcmp(subStructToGrps , '') % do recursive search
grp = jpa_getSubstruct(sjinfo, 'PK');
if isempty(grp); error('Could not find PK!');end
else % try to read given subStruct
grp = sjinfo;
subStructToGrps = textscan(subStructToGrps,'%s','delimiter','.');
subStructToGrps = subStructToGrps{1,1};
for i=1:1:length(subStructToGrps)
grp = grp.(subStructToGrps{i});
end
end
catch ME
% could not get STID or PK
disp(ME.identifier);
error('Structure of sjinfo is wrong!');
end
% convert to Strings if necessary
if isa(ids, 'numeric')
ids = textscan(sprintf('%i\n',ids'),'%s');
ids = ids{1};
end
if isa(grp, 'numeric')
grp = textscan(sprintf('%i\n',grp'),'%s');
grp = grp{1};
end
% convert to vertical vector if necessary
[a, b] = size(ids);
if b>a
ids = transpose(ids);
end
[a, b] = size(grp);
if b>a
grp = transpose(grp);
end
% convert to cellstr if necessary
if ~iscell(grp)
grp = cellstr(grp);
end
%% get Covariates
% Loop through all Covariates
for i=1:length(covarNames)
% check if field exists
cov = jpa_getSubstruct(sjinfo, covarNames{i});
if ~isempty(cov)
covarsc{i} = cov;
covNotEmpty(i) = true;
else % if not -> skip
disp(strcat('WARNING: Covariate "',covarNames{i},'" does not exist! Skipping...'));
end
end
end
%------------- END CODE --------------