Interpolation over datetime for 2 arrays
17 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Ameer Hamza
2020-12-26
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 个评论
Ameer Hamza
2020-12-26
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?
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Time Series Objects 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!