Interpolation over datetime for 2 arrays

10 ビュー (過去 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 Center および File ExchangePolynomials についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by