Clear Filters
Clear Filters

Interpolation over datetime for 2 arrays

16 views (last 30 days)
I need to interpolate over some data but I'm not able to do it myself. Right now I've got data for CGM measurement with its corresponding date and time (sortedCGM.mat) and HR measurement with its corresponding date and time (sortedData.mat). I need to get an interpolated HR at every CGM measurement.
The code I currently use is the following:
int_CGM = interp1(sortedCGM(:,1),sortedCGM(:,2),sortedData(:,2), 'spline');
Here I converted the date and time to datenum values but it stil gave the following error:
Error using interp1>reshapeAndSortXandV (line 435)
X must be a vector of type double or single.
Error in interp1 (line 128)
[X,V,orig_size_v] = reshapeAndSortXandV(X,V);
Can anybody help me?

Accepted Answer

Ameer Hamza
Ameer Hamza on 26 Dec 2020
Your data is not directly in the correct format for interp1(). You first need to pre-process your data. For example
CGM_date = datetime([sortedCGM{:,2}], 'InputFormat', 'dd-MM-yyyy HH:mm');
CGM_data = [sortedCGM{:,1}];
% remove the nan values.
nan_mask = isnan(CGM_data);
CGM_date = CGM_date(~nan_mask);
CGM_data = CGM_data(~nan_mask);
Data_date = datetime(sortedData(:,2), 'InputFormat', 'yyyy-MM-dd HH:mm:ss');
Data_data = [sortedData{:,1}];
int_CGM = interp1(CGM_date, CGM_data, Data_date)
Also see retime().
Tycho Maas
Tycho Maas on 26 Dec 2020
Thanks, that seems to work!
However one more question, how can I include the new interpolated HR data in the sortedCGM array? So the third column contains the new interpolated values. Because right now when I look at the int_CGM data in my workspace, it contains doubles which are all NaN.
Ameer Hamza
Ameer Hamza on 26 Dec 2020
All NaNs? or some non-NaN values? The interpolation can only interpolate values that are inside the range given in CGM_date. Beyond that, you will need to extrapolate. For example
int_CGM = interp1(CGM_date, CGM_data, Data_date, 'linear', 'extrap')
Also, how do you want to add this as the 3rd column of sortedCGM. You interpolated the values at dates given in sortedData. Do you want to add the result as the third column of sortedData?

Sign in to comment.

More Answers (0)




Community Treasure Hunt

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

Start Hunting!