-
Notifications
You must be signed in to change notification settings - Fork 2
/
importSession.m
155 lines (136 loc) · 7.76 KB
/
importSession.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
147
148
149
150
151
152
153
154
155
function sessionData = importSession(sessionDirectory)
% List directories and files in session directory
directoryList = dir(sessionDirectory);
% Error if session directory does not exist
if isempty(directoryList)
error('Session directory is empty or does not exist.');
end
% Error if Session.xml not present
invalid = true;
for directoryIndex = 1:length(directoryList)
if strcmp('Session.xml', directoryList(directoryIndex).name)
invalid = false;
end
end
if invalid
warning('Session.xml not found. This may because the specified directory is not a session directory, or because the logging has not yet completed.');
end
% Loop through each device directory
for directoryIndex = 1:length(directoryList)
% Skip the first 2 results which are always '.' and '..'
if directoryIndex < 3
continue;
end
% Skip if not directory
if ~directoryList(directoryIndex).isdir
continue;
end
% List files in device directory
directoryPath = [sessionDirectory filesep directoryList(directoryIndex).name filesep];
fileList = dir(directoryPath);
% Add each CSV file to data structure
deviceName = formatFieldName(directoryList(directoryIndex).name);
for fileIndex = 1:length(fileList)
filePath = [directoryPath fileList(fileIndex).name];
% Skip if not CSV file
[path, name, extension] = fileparts(filePath);
if ~strcmp('.csv', extension)
continue;
end
% Read CSV headings
fileID = fopen(filePath);
csvHeadings = strsplit(fgets(fileID), ',');
fclose(fileID);
% Read CSV file
fileName = formatFieldName(name);
csvData = dlmread(filePath, ',', 1, 0);
% Remove repeated samples
csvData = unique(csvData, 'rows');
% Add each column to data structure
for headingIndex = 1:length(csvHeadings)
heading = formatFieldName(csvHeadings{headingIndex});
sessionData.(deviceName).(fileName).(heading) = csvData(:,headingIndex);
end
end
end
% Add device names to data structure
deviceNames = fieldnames(sessionData);
sessionData.deviceNames = deviceNames;
numberOfDevices = length(deviceNames);
sessionData.numberOfDevices = numberOfDevices;
% Add specific vector/matrix fields as combined CSV columns
for deviceIndex = 1:sessionData.numberOfDevices
deviceName = sessionData.deviceNames{deviceIndex};
if isfield(sessionData.(deviceName), 'analogue')
sessionData.(deviceName).analogue.vector = [sessionData.(deviceName).analogue.channel1, ...
sessionData.(deviceName).analogue.channel2, ...
sessionData.(deviceName).analogue.channel3, ...
sessionData.(deviceName).analogue.channel4, ...
sessionData.(deviceName).analogue.channel5, ...
sessionData.(deviceName).analogue.channel6, ...
sessionData.(deviceName).analogue.channel7, ...
sessionData.(deviceName).analogue.channel8];
end
if isfield(sessionData.(deviceName), 'earth')
sessionData.(deviceName).earth.vector = [sessionData.(deviceName).earth.x, ...
sessionData.(deviceName).earth.y, ...
sessionData.(deviceName).earth.z];
end
if isfield(sessionData.(deviceName), 'euler')
sessionData.(deviceName).euler.vector = [sessionData.(deviceName).euler.roll, ...
sessionData.(deviceName).euler.pitch, ...
sessionData.(deviceName).euler.yaw];
end
if isfield(sessionData.(deviceName), 'linear')
sessionData.(deviceName).linear.vector = [sessionData.(deviceName).linear.x, ...
sessionData.(deviceName).linear.y, ...
sessionData.(deviceName).linear.z];
end
if isfield(sessionData.(deviceName), 'matrix')
sessionData.(deviceName).matrix.matrix = zeros(3,3,length(sessionData.(deviceName).matrix.time));
sessionData.(deviceName).matrix.matrix(1,1,:) = sessionData.(deviceName).matrix.xx;
sessionData.(deviceName).matrix.matrix(2,1,:) = sessionData.(deviceName).matrix.xy;
sessionData.(deviceName).matrix.matrix(3,1,:) = sessionData.(deviceName).matrix.xz;
sessionData.(deviceName).matrix.matrix(1,2,:) = sessionData.(deviceName).matrix.yx;
sessionData.(deviceName).matrix.matrix(2,2,:) = sessionData.(deviceName).matrix.yy;
sessionData.(deviceName).matrix.matrix(3,2,:) = sessionData.(deviceName).matrix.yz;
sessionData.(deviceName).matrix.matrix(1,3,:) = sessionData.(deviceName).matrix.zx;
sessionData.(deviceName).matrix.matrix(2,3,:) = sessionData.(deviceName).matrix.zy;
sessionData.(deviceName).matrix.matrix(3,3,:) = sessionData.(deviceName).matrix.zz;
end
if isfield(sessionData.(deviceName), 'quaternion')
sessionData.(deviceName).quaternion.vector = [sessionData.(deviceName).quaternion.w, ...
sessionData.(deviceName).quaternion.x, ...
sessionData.(deviceName).quaternion.y, ...
sessionData.(deviceName).quaternion.z];
end
if isfield(sessionData.(deviceName), 'sensors')
sessionData.(deviceName).sensors.gyroscopeVector = [sessionData.(deviceName).sensors.gyroscopeX, ...
sessionData.(deviceName).sensors.gyroscopeY, ...
sessionData.(deviceName).sensors.gyroscopeZ];
sessionData.(deviceName).sensors.accelerometerVector = [sessionData.(deviceName).sensors.accelerometerX, ...
sessionData.(deviceName).sensors.accelerometerY, ...
sessionData.(deviceName).sensors.accelerometerZ];
sessionData.(deviceName).sensors.magnetometerVector = [sessionData.(deviceName).sensors.magnetometerX, ...
sessionData.(deviceName).sensors.magnetometerY, ...
sessionData.(deviceName).sensors.magnetometerZ];
end
end
end
function fieldName = formatFieldName(originalText)
% Remove trailing parentheses
splitOriginalText = strsplit(originalText, '(');
formattedOriginalString = splitOriginalText{1};
% Remove non-alphanumeric characters
formattedOriginalString(~ismember(formattedOriginalString, ['0':'9', 'A':'Z', 'a':'z'])) = ' ';
% Create lower camel case string
words = lower(strsplit(formattedOriginalString, ' '));
fieldName = words{1};
for wordIndex = 2:length(words)
if length(words{wordIndex}) == 0
continue;
end
words{wordIndex}(1) = upper(words{wordIndex}(1));
fieldName = [fieldName words{wordIndex}];
end
end