The sample points must be finite.

조회 수: 84 (최근 30일)
jie hu
jie hu 2023년 6월 20일
댓글: jie hu 2023년 6월 21일
I like to interpolate a sample data on a new timeline by useing interp1, saying "The sample points must be finite."
data_origin=readtable('data_origin.csv');
time=data_origin.Var1;
elev=data_origin.Var2;
%% note the start date in raw data is {'05-Jan-2020 17:40:00'}
%% end date in raw data is {'09-Nov-2020 16:20:00'}
timeline = ( datetime(2020,1,5) + minutes((1060:25:24*60*310-5*92)) ).' ;
%% interpolate schism result to 'obs_tide_time'
interp_elev = interp1(datenum(time(:)),double(elev(:)),datenum(timeline(:)),'linear','extrap');

채택된 답변

Askic V
Askic V 2023년 6월 20일
Most likely, you're getting the error because you have missing entries in the data.
websave("filename.csv","https://www.mathworks.com/matlabcentral/answers/uploaded_files/1415214/data_origin.csv");
data_origin = readtable("filename.csv");
timeData = data_origin.Var1;
values = data_origin.Var2;
% Convert datetime to numerical values
timestamps = datenum(timeData);
% Remove NaN or Inf values from the arrays
validIndices = isfinite(timestamps);
timestamps = timestamps(validIndices);
values = values(validIndices);
data_origin(188:190,:)
ans = 3×2 table
Var1 Var2 ____________________ ____ 08-Jan-2020 23:35:00 0.44 NaT 0.47 09-Jan-2020 00:25:00 0.5
% Is there missing entries in the data:
missing_entries = any(isnat(data_origin.Var1) | isnan(data_origin.Var2));
if missing_entries
% remove mising entries
data_origin = rmmissing(data_origin);
end
% Define new extrapolation range
extrap_start = datenum('05-Jan-2020 17:10:00');
extrap_end = datenum('10-Nov-2020 00:20:00');
% Convert new timestamps to numerical values
extrap_timestamps = linspace(extrap_start, extrap_end, numel(values));
% Perform linear extrapolation using interp1
extrap_values = interp1(timestamps, values, extrap_timestamps, 'linear', 'extrap');
% Convert numerical timestamps back to datetime format
extrap_datetime = datetime(extrap_timestamps, 'ConvertFrom', 'datenum');
plot(extrap_datetime, extrap_values)
  댓글 수: 1
jie hu
jie hu 2023년 6월 21일
Really appreciated, Askic!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Time Series Objects에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by