Interpolation over datetime for 2 arrays

조회 수: 9 (최근 30일)
Tycho Maas
Tycho Maas 2020년 12월 26일
댓글: Ameer Hamza 2020년 12월 26일
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?

채택된 답변

Ameer Hamza
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
Tycho Maas
Tycho Maas 2020년 12월 26일
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 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 CenterFile Exchange에서 Polynomials에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by