Search code examples
matlabplotrangesubsetmatlab-figure

Plot with discrete X-axis range in MATLAB


I have a numeric array called samples with size [1 250]. I want to plot it such that points in range [100 110] and [200 350] are not displayed on the plot.


Solution

  • See comments inside the code:

    function q47150844
    %% Omit Y values in a certain range:
    % Generate some data:
    samples = randi(500,1,250);
    % Choose valid points:
    valid = (samples < 100) | (samples > 110 & samples < 200) | (samples > 350);
    % Set invalid points to NaN:
    samp_proc = samples; samp_proc(~valid) = NaN;
    % Plot the remaining data:
    figure(); plot(samp_proc,'.'); hold on;
    % Plot the rejection limits for verification:
    plot([0 250],[100 100],':r',[0 250],[110 110],':r',...
         [0 250],[200 200],':r',[0 250],[350 350],':r');
    %% Omit X values in a certain range:
    xrange = 1:numel(samples);
    valid = (xrange < 100) | (xrange > 110 & xrange < 200) | (xrange > 350);
    xrange(~valid) = NaN;
    figure(); plot(xrange,samples,'.'); hold on;
    plot([100 100],ylim,':r',[110 110],ylim,':r',...
         [200 200],ylim,':r',[350 350],ylim,':r');
    

    Results:

    Y-value removal

    X-value removal