Clear Filters
Clear Filters

The sample points must be finite.

112 views (last 30 days)
jie hu
jie hu on 20 Jun 2023
Commented: jie hu on 21 Jun 2023
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');

Accepted Answer

Askic V
Askic V on 20 Jun 2023
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)

More Answers (0)

Community Treasure Hunt

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

Start Hunting!