Skip to content

Commit e103d87

Browse files
author
Enric Sala
committed
Allow to create a series from tables or timetables
1 parent 9ffedf3 commit e103d87

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ series = Series('weather') ...
9191
.fields('temperature', [10.5, 11.2, 9.7]) ...
9292
.time(datetime('now', 'TimeZone', 'local') - [0, 1, 2]);
9393
94+
% Create a series from an existing timetable
95+
ttable = timetable(time, variables, etc);
96+
series = Series('weather') ...
97+
.tags('city', 'helsinki', 'country', 'finland') ...
98+
.import(ttable);
99+
94100
% Save the series
95101
influxdb.writer() ...
96102
.append(series, etc) ...

influxdb-client/Series.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,19 @@
5454
end
5555
end
5656

57+
% Import data from other structures
58+
function obj = import(obj, data)
59+
if istimetable(data) || istable(data)
60+
insert = @(x) obj.field(x, data.(x));
61+
cellfun(insert, data.Properties.VariableNames);
62+
if istimetable(data)
63+
obj.time(data.Properties.RowTimes);
64+
end
65+
else
66+
error('unsupported data structure');
67+
end
68+
end
69+
5770
% Format to Line Protocol
5871
function lines = toLine(obj, precision)
5972
time_length = length(obj.Time);

tests/SeriesTest.m

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,31 @@ function time_supports_different_precisions(test)
231231
test.verifyTrue(endsWith(line, exp));
232232
end
233233
end
234+
235+
function imports_fields_from_table(test)
236+
props = {test.Temperature, test.WindDirection};
237+
names = {'temperature', 'wind_direction'};
238+
data = table(props{:}, 'VariableNames', names);
239+
s = Series('weather') ...
240+
.time(test.Time) ...
241+
.import(data);
242+
exp = [ ...
243+
'weather temperature=24.3,wind_direction="north" 1529933525520' newline ...
244+
'weather temperature=-3.5,wind_direction="west" 1529933581618'];
245+
test.verifyEqual(s.toLine(), exp);
246+
end
247+
248+
function imports_time_and_fields_from_timetable(test)
249+
props = {test.Temperature, test.WindDirection};
250+
names = {'temperature', 'wind_direction'};
251+
data = timetable(test.Time, props{:}, 'VariableNames', names);
252+
s = Series('weather') ...
253+
.import(data);
254+
exp = [ ...
255+
'weather temperature=24.3,wind_direction="north" 1529933525520' newline ...
256+
'weather temperature=-3.5,wind_direction="west" 1529933581618'];
257+
test.verifyEqual(s.toLine(), exp);
258+
end
234259
end
235260

236261
end

0 commit comments

Comments
 (0)