Interpolation over datetime for 2 arrays
17 views (last 30 days)
Show older comments
Hi,
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?
0 Comments
Accepted Answer
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().
2 Comments
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?
More Answers (0)
See Also
Categories
Find more on Polynomials in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!