replace NaN value without disturb or remove important peak

3 views (last 30 days)
Here im attached my example data and plot image of the data, i have problem to replace the NaN value without disturb the ploting trend or remove important peak. Can anyone help me to solve this problem?
If you see from data im attach, the NaN value located something at center and the end of the data. i want to make the ploting to be continuous without remove number of data and try to keep the trend also important peak.

Answers (2)

Star Strider
Star Strider on 9 Apr 2025
Edited: Star Strider on 12 Apr 2025
One option is to use the fillmissing function and then plot the filled segments appropriately —
M1 = readmatrix('41679.2500000000.txt');
X1 = linspace(0, numel(M1), numel(M1)).';
FM1 = fillmissing(M1, 'linear');
Lv = isnan(M1);
nnz(Lv)
ans = 57000
Lv1 = strfind(Lv.', [0 1]).'; % Segment Start Indices
Lv2 = [strfind(Lv.', [1 0]); numel(M1)]; % Segment End Indices
figure
plot(X1, M1)
hold on
for k = 1:numel(Lv1)
idxrng = Lv1(k) : Lv2(k);
plot(X1(idxrng), FM1(idxrng), '--r')
end
hold off
grid
.
EDIT — (9 Apr 2025 at 11:43)
Corrected typographical errors.
EDIT — (12 Apr 2026 at 13:23)
The interpolated vector ‘FM1’ has all the necessary values and no NaN gaps —
figure
plot(X1, FM1)
grid
.

Thorsten
Thorsten on 10 Apr 2025
y = readmatrix('41679.2500000000.txt');
x = 1:numel(y);
yi = interp1(x(~isnan(y)), y(~isnan(y)), x, 'linear', 'extrap');
plot(x, yi)

Community Treasure Hunt

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

Start Hunting!