-
Notifications
You must be signed in to change notification settings - Fork 0
/
writeStorageFile.m
64 lines (53 loc) · 2.09 KB
/
writeStorageFile.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
%-------------------------------------------------------------------------%
% Copyright (c) 2021 Modenese L. %
% Author: Luca Modenese, 2021 %
% email: l.modenese@imperial.ac.uk %
% ----------------------------------------------------------------------- %
function writeStorageFile(data_struct, sto_file, str_data_description)
% check on the data structure
if ~isfield(data_struct,'data') || ~isfield(data_struct,'colheaders')
error(['The function writeStorageFile needs as input a dataStruct with fields ''colheaders'' and ''data''.',...
'The number of columns of DataStruct.data has to equalize the number of headers.']);
end
% defines local variables and their size
colheaders = data_struct.colheaders;
data = data_struct.data;
% sizes of data
[N_rows, N_columns] = size(data);
% check on consistency of the structure data
if size(colheaders,2)~=N_columns
error('The number of column headers is not consistent with the number of data rows.')
end
% file name
[~,name,ext] = fileparts(sto_file);
sto_file_name = [name,ext];
% open file
fid = fopen(sto_file,'w');
% % Write Header
fprintf(fid,'%s\n',sto_file_name);
fprintf(fid,'%s\n','version=1');
fprintf(fid,'%s\n',['nRows=',num2str(N_rows)]);
fprintf(fid,'%s\n',['nColumns=',num2str(N_columns)]);
fprintf(fid,'%s\n','inDegrees=no');
fprintf(fid,'\n');
fprintf(fid,'%s\n',['This file contains ',str_data_description, '.']);
fprintf(fid,'\n');
fprintf(fid,'%s\n','endheader');
% writing the column headers while generating the format for printing the
% data
format_string ='';
for n_headers = 1:N_columns
if n_headers == N_columns
% prints header
fprintf(fid,'%s\n',colheaders{N_columns});
% builds up format string
format_string = [format_string,'%-14.14f\n']; %#ok<*AGROW>
else
fprintf(fid,'%s\t', colheaders{n_headers});
format_string = [format_string,'%-14.14f\t'];
end
end
% writing the data in OpenSim format
fprintf(fid, format_string, data');
fclose all;
end