MATLAB: Are the axis limits set based on x-values that correspond to NaN y-values in the PLOT function in MATLAB 7.10 (R2010a)

MATLAB

When I execute the following:
a = [1 2 3 4 100];
b = [1 2 3 4 NaN];
plot(a,b)
I get a figure where the x-axis goes from 1 to 100. I want the x-axis to only show from 1 to 4 since there is really no x data out of the 1 to 4 range.

Best Answer

  • The ability to set axis limits without the consideration of y-values that correspond to NaN x-values is NaN is not available in MATLAB.
    To work around this issue use a function provided below :
    function plotNoNaN(a,b)
    indices = find(~isnan(b));
    plot(a(indices),b(indices));