|
81 | 81 | methods(Static)
|
82 | 82 | % Convert a series result to an object
|
83 | 83 | function obj = from(serie)
|
84 |
| - name = serie.name; |
85 |
| - columns = serie.columns; |
| 84 | + fields = serie.columns; |
86 | 85 | values = serie.values;
|
87 | 86 |
|
88 |
| - % Check if empty result |
89 |
| - if isempty(columns) || isempty(values) |
90 |
| - warning(['serie ''' name ''' is empty']); |
91 |
| - obj = []; |
92 |
| - return |
| 87 | + % Obtain the name if present |
| 88 | + if isfield(serie, 'name') |
| 89 | + name = serie.name; |
| 90 | + else |
| 91 | + name = ''; |
93 | 92 | end
|
94 | 93 |
|
95 |
| - % Extract the tags if present |
| 94 | + % Obtain the tags if present |
96 | 95 | if isfield(serie, 'tags')
|
97 | 96 | tags = serie.tags;
|
98 | 97 | else
|
99 | 98 | tags = struct();
|
100 | 99 | end
|
101 | 100 |
|
| 101 | + % Check if the series is empty |
| 102 | + if isempty(fields) || isempty(values) |
| 103 | + warning(['serie "' name '" is empty']); |
| 104 | + obj = []; |
| 105 | + return |
| 106 | + end |
| 107 | + |
102 | 108 | % Prepare the values in a cell format
|
103 | 109 | N = size(values, 1);
|
104 |
| - fields = columns(2:end); |
105 | 110 | if iscell(values)
|
106 |
| - % Implies there are non-numeric values |
107 |
| - C = length(values{1}); |
108 |
| - celled = cell(N, C); |
| 111 | + % There are non-numeric values |
| 112 | + celled = cell(N, length(values{1})); |
109 | 113 | for i = 1:N
|
110 | 114 | row = values{i};
|
111 | 115 | if iscell(row)
|
112 |
| - % The row contains non-numeric values |
113 | 116 | celled(i, :) = row;
|
114 | 117 | else
|
115 |
| - % The row is all numeric, or NaN |
116 | 118 | celled(i, :) = num2cell(row);
|
117 | 119 | end
|
118 | 120 | end
|
119 |
| - time = SeriesResult.toDatetime(cell2mat(celled(:, 1))); |
120 | 121 | else
|
121 | 122 | % All values are numeric
|
122 |
| - C = size(values, 2); |
123 |
| - time = SeriesResult.toDatetime(values(:, 1)); |
124 | 123 | celled = num2cell(values);
|
125 | 124 | end
|
126 | 125 |
|
127 |
| - % Format the values as name/value structs |
128 |
| - for i = C:-1:2 |
129 |
| - field = fields{i - 1}; |
| 126 | + % Check if the first field is the time |
| 127 | + if strcmp('time', fields{1}) |
| 128 | + timestamps = cell2mat(celled(:, 1)); |
| 129 | + time = SeriesResult.toDatetime(timestamps); |
| 130 | + fields = fields(2:end); |
| 131 | + celled = celled(:, 2:end); |
| 132 | + else |
| 133 | + time = []; |
| 134 | + end |
| 135 | + |
| 136 | + % Format the fields as structs |
| 137 | + C = size(celled, 2); |
| 138 | + for i = C:-1:1 |
| 139 | + field = fields{i}; |
130 | 140 | column = celled(:, i);
|
131 | 141 | if all(cellfun(@(x) isnumeric(x), column))
|
132 | 142 | % Convert to a numeric array
|
|
147 | 157 | % Convert to a nested char cell
|
148 | 158 | value = {column};
|
149 | 159 | end
|
150 |
| - props(i - 1) = struct('field', field, 'value', value); |
| 160 | + props(i) = struct('field', field, 'value', value); |
151 | 161 | end
|
152 | 162 |
|
153 | 163 | % Create the series result
|
|
0 commit comments