Finding the difference in timeseries values

2 views (last 30 days)
david crowley
david crowley on 3 Apr 2021
Answered: Peter Perkins on 7 Dec 2021
If I have the below timetable, how do I find the difference between values? Diff() doesn't work. I've tried diff(TableName.Variables), however that removes the Time column and VariableNames.
Time Humidity TemperatureF PressureHg
____________________ ________ ____________ __________
15-Nov-2015 00:00:00 48.9 51.45 29.61
15-Nov-2015 06:00:00 48.9 51.45 29.6
15-Nov-2015 12:00:00 49.025 51.45 29.61
15-Nov-2015 18:00:00 48.9 51.225 29.607
16-Nov-2015 00:00:00 48.5 51.4 29.61

Answers (2)

Star Strider
Star Strider on 4 Apr 2021
This is kludgy, however I can find no other way to do what you want:
C = {'15-Nov-2015 00:00:00' 48.9 51.45 29.61
'15-Nov-2015 06:00:00' 48.9 51.45 29.6
'15-Nov-2015 12:00:00' 49.025 51.45 29.61
'15-Nov-2015 18:00:00' 48.9 51.225 29.607
'16-Nov-2015 00:00:00' 48.5 51.4 29.61};
T1 = cell2table(C, 'VariableNames',{'Time','Humidity','TemperatureF','PressureHg'}) % Create Table From Data
T1.Time = datetime(T1{:,1}, 'InputFormat','dd-MMM-yyyy HH:mm:ss'); % Create Table From Data
DiffArray = diff(T1{:,2:4}) % Column Differences
DT = table(datetime(diff(datenum(T1{:,1})),'ConvertFrom','datenum','Format','HH:mm'),'VariableNames',{'DiffTime'}); % Time Differences
T2 = [DT array2table(DiffArray, 'VariableNames',{'DiffHumidity','DiffTemperatureF','DiffPressureHg'})]
producing:
T2 =
4×4 table
DiffTime DiffHumidity DiffTemperatureF DiffPressureHg
________ ____________ ________________ ______________
06:00 0 0 -0.01
06:00 0.125 0 0.01
06:00 -0.125 -0.225 -0.003
06:00 -0.4 0.175 0.003
.

Peter Perkins
Peter Perkins on 7 Dec 2021
David, you are correct that diff(TableName.Variables) removes the times and the variable names, in fact, it returns a numeric matrix not a timetable!
Applying diff gives you one fewer rows, so one way or another you need to create a new timetable. If you are only diff'ing one variable, then
ttDiff = timetable(diff(tt.X),'RowTimes',tt.Time(2:end),'VariableNames',{'X_diff'})
is one way to go. For three variables (maybe even for just one in this case), varfun is probably the best:
>> tt
tt =
5×3 timetable
Time Humidity TemperatureF PressureHg
____________________ ________ ____________ __________
15-Nov-2015 00:00:00 48.9 51.45 29.61
15-Nov-2015 06:00:00 48.9 51.45 29.6
15-Nov-2015 12:00:00 49.025 51.45 29.61
15-Nov-2015 18:00:00 48.9 51.225 29.607
16-Nov-2015 00:00:00 48.5 51.4 29.61
>> ttDiff = varfun(@diff,tt)
ttDiff =
4×3 timetable
Time diff_Humidity diff_TemperatureF diff_PressureHg
____________________ _____________ _________________ _______________
15-Nov-2015 00:00:00 0 0 -0.01
15-Nov-2015 06:00:00 0.125 0 0.01
15-Nov-2015 12:00:00 -0.125 -0.225 -0.003
15-Nov-2015 18:00:00 -0.4 0.175 0.003
There's also a long example in the doc that talks about "how to perform calculations by using the numeric and categorical data that the table contains" at length:

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!