简体   繁体   中英

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.

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值移除

X值移除

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM