a b/MATLAB/private/sto2Mat.m
1
%-------------------------------------------------------------------------%
2
% Copyright (c) 2019 Modenese L.                                          %
3
%                                                                         %
4
% Licensed under the Apache License, Version 2.0 (the "License");         %
5
% you may not use this file except in compliance with the License.        %
6
% You may obtain a copy of the License at                                 %
7
% http://www.apache.org/licenses/LICENSE-2.0.                             %
8
%                                                                         % 
9
% Unless required by applicable law or agreed to in writing, software     %
10
% distributed under the License is distributed on an "AS IS" BASIS,       %
11
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or         %
12
% implied. See the License for the specific language governing            %
13
% permissions and limitations under the License.                          %
14
%                                                                         %
15
%    Author:   Luca Modenese                                              %
16
%    email:    l.modenese@imperial.ac.uk                                  % 
17
% ----------------------------------------------------------------------- %
18
% This script converts a .sto file generated by OpenSim analyses in a
19
% matlab structure "MatStruct" with fields "colheaders" and "data".
20
% This is a convenient script to robustly read OpenSim files.
21
%
22
% INPUTS:
23
%           - Storage_file: a .sto file, including path, e.g. from static
24
%           optimization or JointReaction analysis.
25
% OUTPUTS:
26
%           -MatStruct: matlab structure with the following fields:
27
%               a) MatStruct.colheaders: cell array of headers of the
28
%                                           .sto file.
29
%               b) MatStruct.data: matrix containing the data that
30
%                                        were stored in the input file.
31
%
32
%
33
% written: 2014/2015 (Griffith University)
34
% last modified: 
35
% 26 Jun 2017: comments (LM)
36
% 29 Nov 2018: changed name - added to MFD Matlab version.
37
% ----------------------------------------------------------------------- %
38
function MatStruct = sto2Mat(storage_file)
39
40
% OpenSim suggested settings
41
import org.opensim.modeling.*
42
OpenSimObject.setDebugLevel(3);
43
44
% read storage file
45
openSimStorage = Storage(storage_file);
46
47
% extract time column and place it in time array
48
timeColumn = ArrayDouble();
49
openSimStorage.getTimeColumn(timeColumn);
50
for n_t = 0:timeColumn.getSize-1
51
    time_vec(n_t+1,1) = timeColumn.getitem(n_t);
52
end
53
54
% converting labels
55
OSLabelsArray = openSimStorage.getColumnLabels;
56
for n_lab = 0:OSLabelsArray.getSize-1
57
    MatStruct.colheaders{n_lab+1} = char(OSLabelsArray.get(n_lab));
58
end
59
60
% converting data
61
dataColumn = ArrayDouble();
62
ind = 0;
63
while openSimStorage.getDataColumn(ind, dataColumn)
64
    for n_d = 0:dataColumn.getSize-1
65
        StorageData(n_d+1,ind+1) = dataColumn.getitem(n_d);
66
    end
67
    ind = ind+1;
68
end
69
MatStruct.data = [time_vec, StorageData];
70
71
% checking that dimensionality of data and colheaders are consistent
72
n_diff = size(MatStruct.colheaders,2)-size(MatStruct.data,2);
73
74
% if not consistent let the user decide what to do
75
if n_diff>0
76
    answ = menu(['In the conversion from storage to MATLAB structure ',num2str(n_diff),' more header(s) than data were found. Removing extra headers (y) or stop (n).'], 'Yes','No');
77
    if answ == 1
78
        MatStruct.colheaders = MatStruct.colheaders(1:size(MatStruct.data,2));
79
    elseif answ == 2
80
        error('Please check your sto files manually.');
81
    end
82
end
83
84
end