interp1() returns a plot with empty sections
0 comentarios
Respuestas (2)
0 comentarios
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.
0 comentarios
Ver también
Categorías
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!