how to solve if interpolated data also contains NaN values
2 views (last 30 days)
Show older comments
I was trying to interpolate 'lat' and 'long' columns but even after interpolation first 4 data are still showing as NaN. For other data, interpolation is ok. I request for help /suggestion for the first 4 data. Thanks!
% Initialize lat_clean and long_clean with NaN values
lat_clean = NaN(size(lat));
long_clean = NaN(size(long));
% Update lat_clean and long_clean if the corresponding values in lat and long are not 9999.9
for i = 1:length(lat)
if (lat(i) ~= 9999.9) && (long(i) ~= 9999.9)
lat_clean(i) = lat(i);
long_clean(i) = long(i);
end
end
% Linear interpolation of NaN data
t_lat = 1:numel(lat_clean);
nan_lat = isnan(lat_clean);
lat_clean(nan_lat) = interp1(t_lat(~nan_lat), lat_clean(~nan_lat), t_lat(nan_lat));
t_long = 1:numel(long_clean);
nan_long = isnan(long_clean);
long_clean(nan_long) = interp1(t_long(~nan_long), long_clean(~nan_long), t_long(nan_long));
% Concatenate data to form new matrix
new_data = [time_numeric, density_dir, speed_dir, temperature_dir, distance_AU, lat_clean, long_clean];
% Write the new data to a text file
NHVOY2_Data = 'NH_VOY2_Data.txt'; % Replace with the desired output file path
dlmwrite(NHVOY2_Data, new_data, 'delimiter', '\t', 'precision', '%.6f');
result showing as
lat_clean =
NaN
NaN
NaN
NaN
-1.4000
-1.2000
-0.9000
-1.0000
-0.3000
5 Comments
Accepted Answer
Chunru
on 13 Mar 2024
a = readmatrix("https://www.mathworks.com/matlabcentral/answers/uploaded_files/1641181/V2_small.txt");
a(a>9999.8) = nan
b = fillmissing(a, 'linear')
0 Comments
More Answers (0)
See Also
Categories
Find more on NaNs in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!