Finding the difference in timeseries values

31 ビュー (過去 30 日間)
david crowley
david crowley 2021 年 4 月 3 日
回答済み: Siddharth Bhutiya 2023 年 3 月 30 日
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

回答 (3 件)

Star Strider
Star Strider 2021 年 4 月 4 日
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 2021 年 12 月 7 日
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:

Siddharth Bhutiya
Siddharth Bhutiya 2023 年 3 月 30 日
If you are using R2023a, then you can now directly call diff on a timetable. Here is an example,
>> tt
tt =
5x3 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
>> diff(tt)
ans =
4x3 timetable
Time Humidity TemperatureF 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
See this documentation page for other arithmetic operations that are now allowed on tables and timetables.

カテゴリ

Help Center および File ExchangeTables についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by