how to solve if interpolated data also contains NaN values

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 Commenti

fillmissing will be a better fit here, instead of interp1.
Also, use a tolerance to compare floating point values. You could also replace the loop with logical indexing.
In any case, please share your data so that we can reproduce the results you obtained. Use the paperclip button to attach.
Thank you. Data for lat is like
1.0e+03 *
9.9999
9.9999
9.9999
9.9999
-0.0014
-0.0012
-0.0009
-0.0010
-0.0003
-0.0005
-0.0011
-0.0006
Please save the data in a file and then upload the file.
A general note - The data displayed is not the same as data stored.
Ismita
Ismita il 13 Mar 2024
Spostato: Sam Chak il 13 Mar 2024
Thank you. In the attached file of the data, last column is for long and second last column is for lat.
thank you so much :)

Accedi per commentare.

 Risposta accettata

a = readmatrix("https://www.mathworks.com/matlabcentral/answers/uploaded_files/1641181/V2_small.txt");
a(a>9999.8) = nan
a = 24x6
1.0e+03 * 1.9920 0.1230 0 0.0372 NaN NaN 1.9920 0.1230 0.0010 0.0372 NaN NaN 1.9920 0.1230 0.0020 0.0372 NaN NaN 1.9920 0.1230 0.0030 0.0372 NaN NaN 1.9920 0.1230 0.0040 0.0372 -0.0014 0.0006 1.9920 0.1230 0.0050 0.0372 -0.0012 0.0008 1.9920 0.1230 0.0060 0.0372 -0.0009 0.0008 1.9920 0.1230 0.0070 0.0372 -0.0010 0.0007 1.9920 0.1230 0.0080 0.0372 -0.0003 -0.0002 1.9920 0.1230 0.0090 0.0372 -0.0005 -0.0002
b = fillmissing(a, 'linear')
b = 24x6
1.0e+03 * 1.9920 0.1230 0 0.0372 -0.0022 -0.0002 1.9920 0.1230 0.0010 0.0372 -0.0020 -0.0000 1.9920 0.1230 0.0020 0.0372 -0.0018 0.0002 1.9920 0.1230 0.0030 0.0372 -0.0016 0.0004 1.9920 0.1230 0.0040 0.0372 -0.0014 0.0006 1.9920 0.1230 0.0050 0.0372 -0.0012 0.0008 1.9920 0.1230 0.0060 0.0372 -0.0009 0.0008 1.9920 0.1230 0.0070 0.0372 -0.0010 0.0007 1.9920 0.1230 0.0080 0.0372 -0.0003 -0.0002 1.9920 0.1230 0.0090 0.0372 -0.0005 -0.0002

Più risposte (0)

Categorie

Richiesto:

il 13 Mar 2024

Commentato:

il 13 Mar 2024

Community Treasure Hunt

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

Start Hunting!

Translated by