stefan@webrtc.org | eb74a37 | 2011-09-02 13:24:38 +0000 | [diff] [blame] | 1 | function parsed = parseLog(filename) |
| 2 | % |
| 3 | % parsed = parseLog(filename) |
| 4 | % Parses a DataLog text file, with the filename specified in the string |
| 5 | % filename, into a struct with each column name as a field, and with the |
| 6 | % column data stored as a vector in that field. |
| 7 | % |
| 8 | % Arguments |
| 9 | % |
| 10 | % filename: A string with the name of the file to parse. |
| 11 | % |
| 12 | % Return value |
| 13 | % |
| 14 | % parsed: A struct containing each column parsed from the input file |
| 15 | % as a field and with the column data stored as a vector in that |
| 16 | % field. |
| 17 | % |
| 18 | |
| 19 | % Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. |
| 20 | % |
| 21 | % Use of this source code is governed by a BSD-style license |
| 22 | % that can be found in the LICENSE file in the root of the source |
| 23 | % tree. An additional intellectual property rights grant can be found |
| 24 | % in the file PATENTS. All contributing project authors may |
| 25 | % be found in the AUTHORS file in the root of the source tree. |
| 26 | |
| 27 | table = importdata(filename, ',', 1); |
andrew@webrtc.org | c853aa1 | 2011-12-08 20:56:53 +0000 | [diff] [blame] | 28 | if ~isstruct(table) |
| 29 | error('Malformed file, possibly empty or lacking data entries') |
| 30 | end |
| 31 | |
| 32 | colheaders = table.textdata; |
| 33 | if length(colheaders) == 1 |
| 34 | colheaders = regexp(table.textdata{1}, ',', 'split'); |
| 35 | end |
| 36 | |
stefan@webrtc.org | eb74a37 | 2011-09-02 13:24:38 +0000 | [diff] [blame] | 37 | parsed = struct; |
| 38 | i = 1; |
andrew@webrtc.org | c853aa1 | 2011-12-08 20:56:53 +0000 | [diff] [blame] | 39 | while i <= length(colheaders) |
stefan@webrtc.org | eb74a37 | 2011-09-02 13:24:38 +0000 | [diff] [blame] | 40 | % Checking for a multi-value column. |
andrew@webrtc.org | c853aa1 | 2011-12-08 20:56:53 +0000 | [diff] [blame] | 41 | m = regexp(colheaders{i}, '([\w\t]+)\[(\d+)\]', 'tokens'); |
stefan@webrtc.org | eb74a37 | 2011-09-02 13:24:38 +0000 | [diff] [blame] | 42 | if ~isempty(m) |
| 43 | % Parse a multi-value column |
| 44 | n = str2double(m{1}{2}) - 1; |
| 45 | parsed.(strrep(m{1}{1}, ' ', '_')) = table.data(:, i:i+n); |
| 46 | i = i + n + 1; |
andrew@webrtc.org | c853aa1 | 2011-12-08 20:56:53 +0000 | [diff] [blame] | 47 | elseif ~isempty(colheaders{i}) |
stefan@webrtc.org | eb74a37 | 2011-09-02 13:24:38 +0000 | [diff] [blame] | 48 | % Parse a single-value column |
andrew@webrtc.org | c853aa1 | 2011-12-08 20:56:53 +0000 | [diff] [blame] | 49 | parsed.(strrep(colheaders{i}, ' ', '_')) = table.data(:, i); |
stefan@webrtc.org | eb74a37 | 2011-09-02 13:24:38 +0000 | [diff] [blame] | 50 | i = i + 1; |
| 51 | else |
andrew@webrtc.org | c853aa1 | 2011-12-08 20:56:53 +0000 | [diff] [blame] | 52 | error('Empty column'); |
stefan@webrtc.org | eb74a37 | 2011-09-02 13:24:38 +0000 | [diff] [blame] | 53 | end |
| 54 | end |