how to not plot NaN but still have same array length
Mostrar comentarios más antiguos
I have y = array of 10000 data. A lot of them are NaN. I dont want to plot NaN but if I use plot(y(~isnan(y)),'o'); my plot only goes up to ~1000. It should still go up to 10000 but not plot those with NaN. How can i do this ?
1 comentario
Mario
el 27 de Jun. de 2026 a las 12:46
just add xlim([0 10000]) after the plot expression without ~isnan
Respuesta aceptada
Más respuestas (1)
Image Analyst
el 25 de Nov. de 2016
Your code basically extracts the non-nan elements and plots only those so it won't go all the way up to 1000. You need to plot x also to have it still go up to 1000. See this little demo:
numPoints = 200; % Make 1000 if you want. I used 200 to make it easier to see what's going on.
% Make sine wave sample data.
x = 1 : numPoints;
period = 200;
y = sin(2 * pi * x / period);
% Introduce 80 nans
nanLocations = randi(length(x), 1, 80);
y(nanLocations) = nan;
% Plot entire array, even the nans, which won't appear.
plot(x, y, 'bo-', 'LineWidth', 2);
grid on;
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')

2 comentarios
atek
el 9 de Feb. de 2018
what if y(1:100) = NaN? Matlab begins plotting at first real value, and I want it to plot NaN's as blank spaces
Walter Roberson
el 9 de Feb. de 2018
Use xlim() to force the left boundary to start at 0
Categorías
Más información sobre Annotations en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!