You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. % Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
  19. %
  20. % Use of this source code is governed by a BSD-style license
  21. % that can be found in the LICENSE file in the root of the source
  22. % tree. An additional intellectual property rights grant can be found
  23. % in the file PATENTS. All contributing project authors may
  24. % be found in the AUTHORS file in the root of the source tree.
  25. table = importdata(filename, ',', 1);
  26. if ~isstruct(table)
  27. error('Malformed file, possibly empty or lacking data entries')
  28. end
  29. colheaders = table.textdata;
  30. if length(colheaders) == 1
  31. colheaders = regexp(table.textdata{1}, ',', 'split');
  32. end
  33. parsed = struct;
  34. i = 1;
  35. while i <= length(colheaders)
  36. % Checking for a multi-value column.
  37. m = regexp(colheaders{i}, '([\w\t]+)\[(\d+)\]', 'tokens');
  38. if ~isempty(m)
  39. % Parse a multi-value column
  40. n = str2double(m{1}{2}) - 1;
  41. parsed.(strrep(m{1}{1}, ' ', '_')) = table.data(:, i:i+n);
  42. i = i + n + 1;
  43. elseif ~isempty(colheaders{i})
  44. % Parse a single-value column
  45. parsed.(strrep(colheaders{i}, ' ', '_')) = table.data(:, i);
  46. i = i + 1;
  47. else
  48. error('Empty column');
  49. end
  50. end