-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsaveSensorDataAsMATFiles.m
More file actions
74 lines (72 loc) · 3.6 KB
/
saveSensorDataAsMATFiles.m
File metadata and controls
74 lines (72 loc) · 3.6 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
function saveSensorDataAsMATFiles
% Load raw sensor data from files and convert them into tables
% The function saves the raw sensor data into two mat files:
% # rawSensorData_train
% # rawSensorData_test
% Copyright (c) 2015, MathWorks, Inc.
if exist('rawSensorData_train.mat','file') && exist('rawSensorData_test.mat','file')
fprintf(1,'rawSensorData_train.mat and rawSensorData_test.mat already exists at location:\n');
disp(['* ', which('rawSensorData_train.mat')]);
disp(['* ', which('rawSensorData_test.mat')]);
disp(' ')
else
%% Load training data from files
activity_labels = {'Walking','WalkingUpstairs','WalkingDownstairs','Sitting','Standing','Laying'};
trainActivity = categorical(importdata('UCI HAR Dataset\train\y_train.txt'),1:6,activity_labels);
trainActivity = mergecats(trainActivity,{'WalkingUpstairs','WalkingDownstairs'},'ClimbingStairs');
trainActivity = reordercats(trainActivity ,{'Laying','Sitting','ClimbingStairs','Standing','Walking'});
% Choose this if you want to only load the total acca and gyro data
filestoload = strcat('UCI HAR Dataset\train\Inertial Signals\',{'*gyro*','total*'});
% Choose this if you want to load all files
% filestoload = strcat('UCI HAR Dataset\train\Inertial Signals\');
disp('Loading training data from files:')
try
dstrain = datastore(filestoload,'TextscanFormats',repmat({'%f'},1,128), 'ReadVariableNames',false);
catch err
if strcmp(err.identifier,'MATLAB:datastoreio:pathlookup:fileNotFound')
error('File not found. Please make sure that you download and extract the data first using ''downloadSensorData'' function')
end
end
dstrain.ReadSize = 'file';
[~,fnames] = cellfun(@fileparts,dstrain.Files,'UniformOutput',false);
iter = 1;
while hasdata(dstrain)
fprintf('Importing: %16s ...',fnames{iter})
M = table2array(read(dstrain));
rawSensorDataTrain.(fnames{iter}) = M;
iter = iter + 1;
fprintf('Done\n')
end
rawSensorDataTrain.trainActivity = trainActivity;
disp(' ')
%% Load test data from files
testActivity = categorical(importdata('UCI HAR Dataset\test\y_test.txt'),1:6,activity_labels);
testActivity = mergecats(testActivity,{'WalkingUpstairs','WalkingDownstairs'},'ClimbingStairs');
testActivity = reordercats(testActivity ,{'Laying','Sitting','ClimbingStairs','Standing','Walking'});
% Choose this if you want to only load the total acc and gyro data
filestoload = strcat('UCI HAR Dataset\test\Inertial Signals\',{'*gyro*','total*'});
% Choose this if you want to load all files
% filestoload = strcat('UCI HAR Dataset\test\Inertial Signals\');
disp('Loading test data from files:')
dstest = datastore(filestoload,'TextscanFormats',repmat({'%f'},1,128),'DatastoreType','tabulartext',...
'ReadVariableNames',false);
[~,fnames] = cellfun(@fileparts,dstest.Files,'UniformOutput',false);
dstest.ReadSize = 'file';
iter = 1;
while hasdata(dstest)
fprintf('Importing: %16s ...',fnames{iter})
M = table2array(read(dstest));
rawSensorDataTest.(fnames{iter}) = M;
iter = iter + 1;
fprintf('Done\n')
end
rawSensorDataTest.testActivity = testActivity;
disp(' ')
%% Saving MAT file with raw data
fprintf('Saving MAT files: rawSensorData_train.mat ...')
save rawSensorData_train.mat -struct rawSensorDataTrain
disp('Done')
fprintf('Saving MAT files: rawSensorData_test.mat ...')
save rawSensorData_test.mat -struct rawSensorDataTest
disp('Done')
end