# Built in look-up style interpolation of timetables for non-numeric data?

6 views (last 30 days)
J. Alex Lee on 4 Mar 2022
Commented: J. Alex Lee on 4 Mar 2022
Is there no built-in function similar to interp1 for numeric data, that can operate on timetables with arbitrary data types?
Some random illustrative data:
x = 1:10; % numeric x values
y = sin(x); % numeric y values
t = datetime()+(1:10); % datetime x values
yNN = "foo"+strings(1,10); % non-numeric y values
yNN(3:6) = "bar"; % non-numeric y values
yNN = 1×10 string array
"foo" "foo" "bar" "bar" "bar" "bar" "foo" "foo" "foo" "foo"
T = timetable(t',yNN',y')
T = 10×2 timetable
Time Var1 Var2 ____________________ _____ ________ 05-Mar-2022 12:20:12 "foo" 0.84147 06-Mar-2022 12:20:12 "foo" 0.9093 07-Mar-2022 12:20:12 "bar" 0.14112 08-Mar-2022 12:20:12 "bar" -0.7568 09-Mar-2022 12:20:12 "bar" -0.95892 10-Mar-2022 12:20:12 "bar" -0.27942 11-Mar-2022 12:20:12 "foo" 0.65699 12-Mar-2022 12:20:12 "foo" 0.98936 13-Mar-2022 12:20:12 "foo" 0.41212 14-Mar-2022 12:20:12 "foo" -0.54402
With interp1, we can take a "series" and interpolate on it with arbitrary (unsorted, repeated) values:
xProbe = [4 4 6 2 1 1 9];
yProbe = interp1(x,y,xProbe,"nearest")
yProbe = 1×7
-0.7568 -0.7568 -0.2794 0.9093 0.8415 0.8415 0.4121
However, it appears we cannot use timetable tools like retime or synchronize to do something similar on timetables...
tProbe = t(xProbe)
tProbe = 1×7 datetime array
08-Mar-2022 12:20:12 08-Mar-2022 12:20:12 10-Mar-2022 12:20:12 06-Mar-2022 12:20:12 05-Mar-2022 12:20:12 05-Mar-2022 12:20:12 13-Mar-2022 12:20:12
R = retime(T,tProbe,"nearest")
Error using timetable/retime (line 142)
Target time vector for synchronization must contain unique times.
One also cannot use interp1 on non-numeric data
% yProbe = interp1(x,yNN,xProbe,"nearest") % uncomment to see it does not work
so that we cannot even do a simple hack like
% yProbe = interp1(seconds(t-datetime()),yNN,seconds(tProbe-datetime()),"nearest");
I can think of several ways to implement what I ultimately want, but is there some built in analog to "interp" function to operate on timetables that I am just not finding?

Peter Perkins on 4 Mar 2022
Edited: Peter Perkins on 4 Mar 2022
You absolutely can use interp1 on a numeric vector over a datetime grid with unsorted, repeated query points:
>> interp1(t,y,tProbe)
ans =
-0.7568 -0.7568 -0.27942 0.9093 0.84147 0.84147 0.41212
retime won't let you do that:
"Target time vector for synchronization must contain unique times."
If you can do this
>> R = retime(T,unique(tProbe),"nearest")
R =
5×2 timetable
Time Var1 Var2
____________________ _____ ________
05-Mar-2022 14:43:18 "foo" 0.84147
06-Mar-2022 14:43:18 "foo" 0.9093
08-Mar-2022 14:43:18 "bar" -0.7568
10-Mar-2022 14:43:18 "bar" -0.27942
13-Mar-2022 14:43:18 "foo" 0.41212
you can then use xProbe:
>> R(t(xProbe),:)
ans =
7×2 timetable
Time Var1 Var2
____________________ _____ ________
08-Mar-2022 14:43:18 "bar" -0.7568
08-Mar-2022 14:43:18 "bar" -0.7568
10-Mar-2022 14:43:18 "bar" -0.27942
06-Mar-2022 14:43:18 "foo" 0.9093
05-Mar-2022 14:43:18 "foo" 0.84147
05-Mar-2022 14:43:18 "foo" 0.84147
13-Mar-2022 14:43:18 "foo" 0.41212
Should retime allow you to use tProbe? Maybe, at least for nearest neighbor and interoplation methods, but not for some others, I will make a note to look into that.
J. Alex Lee on 4 Mar 2022
Oops, I hadn't thanked you yet @Peter Perkins for the answer. Notwithstanding the back-and-forth about the context and details, appreciate your help getting around this problem and significantly improving on the work-around I was employing before this answer.