-
Notifications
You must be signed in to change notification settings - Fork 6
/
rosArrayToMatlabArray.m
117 lines (105 loc) · 4.2 KB
/
rosArrayToMatlabArray.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
% rosArrayToMatlabArray Converts an array of values in a CSV column to an array
% rosArrayToMatlabArray(input) Converts a string read from a CSV file generated by the bag2csv.py
% script to a Matlab array. For instance, converts all of the ranges in a laser scanner message to
% an array.
%
% rosArrayToMatlabArray(input, use_parallel) Turn the use of parallel computing features on or off.
% Copyright (c) 2014 David Anthony
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
function [values] = rosArrayToMatlabArray(input, use_parallel)
if(isempty(input))
values = [];
return;
end
% Sets the optional parallel toolbox features to default to on
if(nargin == 1)
use_parallel = true;
end
% Pre-allocate output variables
num_samples = size(input, 1);
values = nan(num_samples, numel(strsplit(input{1}, '`')));
max_col = size(values, 2);
if(use_parallel == true)
% Go through all the passed in strings, split on the underscores, and convert them to numeric
% values. We first place the results in a cell array, so if the extracted arrays are not
% of uniform length, we can merge them into a multidimensional array later.
converted_samples = cell(num_samples, 1);
parfor sample_idx = 1:num_samples
samples = {};
% Use textscan to split CSV fields on the underscore
if(~isempty(input{sample_idx}))
samples = textscan(input{sample_idx}, '%s', 'delimiter', '`');
% textscan returns a cell array within a cell. Undo this redirection so we have an nx1
% cell array
samples = samples{1}';
sample_val = nan(size(samples));
else
sample_val = [];
end
% Go through the extracted results and convert to floating point numbers
for sample_val_idx = 1:numel(sample_val)
new_sample = sscanf(samples{sample_val_idx}, '%f');
if(isempty(new_sample))
sample_val(sample_val_idx) = nan;
else
sample_val(sample_val_idx) = new_sample;
end
end
converted_samples{sample_idx} = sample_val;
end
% Get the size of the arrays that we extracted
lengths = cellfun('length', converted_samples);
max_col = max(lengths);
% If all of the fields were the same length, we can convert them into a matrix using
% cell2mat() directly. Otherwise, we will go through each extracted array, pad it out to
% the maximum length, and place it in a matrix of the maximum size.
if(min(lengths) == max_col)
values = cell2mat(converted_samples);
else
values = nan(num_samples, max(lengths));
parfor sample_idx = 1:numel(converted_samples)
if(~isempty(converted_samples{sample_idx}))
values(sample_idx, :) = padarray(...
converted_samples{sample_idx}, ...
[0, max_col - numel(converted_samples{sample_idx})], ...
nan, ...
'post');
else
values(sample_idx, :) = nan(size(values(sample_idx, :)));
end
end
end
else
% Same as parallel case, but we dynamically grow and shrink the output array
for sample_idx = 1:num_samples
samples = textscan(input{sample_idx}, '%s', 'delimiter', '``');
samples = samples{1}';
sample_val = nan(size(samples));
for sample_val_idx = 1:numel(sample_val)
sample_val(sample_val_idx) = sscanf(samples{sample_val_idx}, '%f');
end
if(numel(sample_val) > max_col)
values = padarray(values, [0, numel(samples) - max_col], nan, 'post');
max_col = numel(samples);
elseif(numel(sample_val) < max_col)
sample_val = padarray(sample_val, [0, max_col - numel(sample_val)], nan, 'post');
end
values(sample_idx, :) = sample_val;
end
end
% Remove any columns of all invalid entries
values(:, all(isnan(values), 1)) = [];
end