interp1() returns a plot with empty sections

22 views (last 30 days)
Ata
Ata on 24 Dec 2025 at 11:18
Edited: Star Strider about 3 hours ago
I am trying to use the interp1() function to first, plot a continuous graph between a variable and time. Then I want to use the plot to look up the value of the variable at every instant. However, my plot appears to have empty sections, which is probably why I get NaN errors in my calculations. I attached my excel file also.
grade_data = readmatrix('grade.xlsx'); % reads the excel file
t_grade_raw = grade_data(:, 1)'; % Extract Time (transpose to row vector)
grade_raw = grade_data(:, 2)'; % Extract Grade (transpose to row vector)
% Interpolating to plot
grade_interp = interp1(t_grade_raw, grade_raw, t, 'linear');
% Safety: Replace any remaining NaNs with 0
grade_interp(isnan(grade_interp)) = 0;
% Graphing
plot(t, grade_interp, 'r-', 'LineWidth', 1.5);

Answers (2)

Star Strider
Star Strider on 24 Dec 2025 at 11:29
Edited: Star Strider 36 minutes ago
The 't' variable seems to be undefined, and we do not have 'grade.xlsx' to work with.
If 't' is beyond the limits of 't_grade_raw', the result will be NaN unless the 'extrap' flag is set, , allowing interp1 to extrapolate.
EDIT -- (25 Dec 2025 at 14:31)
Example --
x = [3:6].'
x = 4×1
3 4 5 6
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
y = 2*x+3
y = 4×1
9 11 13 15
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
xq = (1:0.5:8).';
yq(:,1) = interp1(x, y, xq, 'linear');
yq(:,2) = interp1(x, y, xq, 'linear', 'extrap');
Result = table(xq, yq(:,1), yq(:,2), VariableNames=["x Interpolant","Interpolated y without'extrap'","Interpolated y with 'extrap'"])
Result = 15×3 table
x Interpolant Interpolated y without'extrap' Interpolated y with 'extrap' _____________ ______________________________ ____________________________ 1 NaN 5 1.5 NaN 6 2 NaN 7 2.5 NaN 8 3 9 9 3.5 10 10 4 11 11 4.5 12 12 5 13 13 5.5 14 14 6 15 15 6.5 NaN 16 7 NaN 17 7.5 NaN 18 8 NaN 19
figure
plot(xq, yq(:,1), '.-')
title("Interpolated y without'extrap'");
grid
axis([min(xq) max(xq) min(yq(:,2)) max(yq(:,2))])
xlabel('X')
ylabel('Y')
figure
plot(xq, yq(:,2), '.-')
title("Interpolated y with 'extrap'")
hold off
grid
axis([min(xq) max(xq) min(yq(:,2)) max(yq(:,2))])
xlabel('X')
ylabel('Y')
.

Umar
Umar about 1 hour ago

Hi @Ata,

I took a look at your code and screenshot of Excel file. The problem is that your t vector goes beyond the range of your grade data (which stops at 1600). When interp1() tries to find values outside [0, 1600], it returns NaN, which you're then replacing with zeros - that's what's creating those flat sections in your plot.

Quick fix - just add 'extrap' to your interp1 call:

grade_interp = interp1(t_grade_raw, grade_raw, t, 'linear', 'extrap');

That's it. This tells MATLAB to extrapolate beyond your data range instead of returning NaN.

To see what's actually happening with your data, run this:

% Diagnostic script - shows the difference between methods
clear; clc;
% Your grade data (from Excel)
t_grade_raw = [0, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600, 
650, 700, 750, 800, 850, 900, 950, 1000, 1050, 1100, 1150, 1200, 1250, 1300, 
1350, 1400, 1450, 1500, 1550, 1600];
grade_raw = [0, -25, 5, 0, -27, -36, 10, -5, 0, -10, 0, 5, -5, 5, -5, 0, 5, 10, -12,   -8,-12, -8, -20, 0, 17, 36, 34, 0, 13, 6, -12, 12, 0];
% Example t vector that extends past your data (adjust this to match yours)
t = 0:5:2000;
fprintf('Data range: [%.0f, %.0f]\n', min(t_grade_raw), max(t_grade_raw));
fprintf('Your t range: [%.0f, %.0f]\n', min(t), max(t));
fprintf('Points outside data: %d (%.1f%%)\n\n', sum(t > max(t_grade_raw)),   
100*sum(t > max(t_grade_raw))/length(t));
% Your current approach
grade_original = interp1(t_grade_raw, grade_raw, t, 'linear');
fprintf('NaNs before replacing with 0: %d\n', sum(isnan(grade_original)));
grade_original(isnan(grade_original)) = 0;
% With extrapolation
grade_extrap = interp1(t_grade_raw, grade_raw, t, 'linear', 'extrap');
fprintf('NaNs with extrapolation: %d\n\n', sum(isnan(grade_extrap)));
% Plot comparison
figure('Position', [100, 100, 1200, 800]);
subplot(2,2,1);
plot(t_grade_raw, grade_raw, 'bo-', 'LineWidth', 1.5, 'MarkerSize', 6);
grid on;
title('Your Raw Grade Data');
xlabel('Distance (m)');
ylabel('Grade (%)');
xlim([min(t), max(t)]);
subplot(2,2,2);
plot(t, grade_original, 'r-', 'LineWidth', 1.5);
hold on;
plot(t_grade_raw, grade_raw, 'bo', 'MarkerSize', 4);
xline(max(t_grade_raw), 'k--');
grid on;
title('Current Method (NaN → 0)');
xlabel('Distance (m)');
ylabel('Grade (%)');
legend('Interpolated', 'Raw Data', 'Data Limit');
subplot(2,2,3);
plot(t, grade_extrap, 'g-', 'LineWidth', 1.5);
hold on;
plot(t_grade_raw, grade_raw, 'bo', 'MarkerSize', 4);
xline(max(t_grade_raw), 'k--');
grid on;
title('With Extrapolation (Recommended)');
xlabel('Distance (m)');
ylabel('Grade (%)');
legend('Interpolated', 'Raw Data', 'Data Limit');
subplot(2,2,4);
plot(t, grade_original, 'r-', 'LineWidth', 1.2, 'DisplayName', 'Current (NaN→0)');
hold on;
plot(t, grade_extrap, 'g-', 'LineWidth', 1.5, 'DisplayName', 'With extrap');
plot(t_grade_raw, grade_raw, 'ko', 'MarkerSize', 3, 'DisplayName', 'Raw Data');
xline(max(t_grade_raw), 'k--', 'DisplayName', 'Data Limit');
grid on;
title('Direct Comparison');
xlabel('Distance (m)');
ylabel('Grade (%)');
legend('Location', 'best');
sgtitle('Grade Interpolation Comparison', 'FontSize', 14);

I ran this with a test t vector (0:5:2000) to demonstrate the issue - see attached. Notice how your current method drops to zero after 1600m (top right plot), while extrapolation continues the trend smoothly (middle left plot). The bottom right comparison shows all methods side-by-side.

If extrapolation doesn't work for your application (maybe you actually want grade to be zero beyond your data), then your current approach is fine. But if you're getting calculation errors from NaNs, this should fix it.

Let me know if this helps or if you have questions about which approach makes sense for your use case.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!