How to force a plot to show trailing NaNs

I have a simple set of values that I display on a bar plot or a line plot. Sometimes, depending on the outcome of steps in the code, some values will be set to NaN.
When they appear in the middle of the data series (ex: [1 2 3 NaN 8 9]), the plot shows 6 "data points", with the 4th not being plotted. However, there is a corresponding x-axis location for this missing NaN point.
However, when the NaN appears at the end of the data series (ex: [1 2 3 6 8 NaN]), the plot will onl show 5 "data points", with the 6th not only non being plotted, but completely missing a corresponding x-axis location. This creates problems, as I need the x-axis to be consistent, and will subsitute the x-axis locations with custom xtick labels that should show all the possible x-axis values. Also, I would like to subsitute a text box to appear where there is missing/NaN data, to add a note about why this is missing. Obviously all of this is not possible if the location for the data point is removed and the series is simply truncated.
Any pointers are appreciated. Thanks.

Réponses (1)

Image Analyst
Image Analyst le 23 Sep 2021
Try this:
% Demo by Image Analyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 25;
fprintf('Beginning to run %s.m ...\n', mfilename)
v = [1 2 nan 3 6 8 5.5 1.5 NaN]
bar(v, 1);
grid on;
xlim([1, length(v)+1]);
yl = ylim;
nanIndexes = find(isnan(v))
for k = 1 : length(v)
if isnan(v(k))
y = 0;
str = 'NaN';
else
y = v(k)
str = sprintf('%.1f', y);
end
text(k, y, str, ...
'Color', 'b', 'FontSize', 25, ...
'FontWeight', 'bold', ...
'VerticalAlignment', 'bottom', ...
'HorizontalAlignment', 'center');
end
g = gcf;
g.WindowState = 'maximized'
fprintf('Done running %s.m.\n', mfilename)

4 commentaires

AR
AR le 24 Sep 2021
Thank you for the answer. I have since realized that I don't think Matlab ignores the trailing NaNs. There was a problem with my data, where the last few data points simply didn't exist. So the problem really is only one of labeling the NaN points.
While this certainly works, what I omitted mentioning was that my data consisted of 2 data-points per x-axis coordinate. For example - ([1,5], [Nan,NaN], [3,2], [0.5,4], [NaN,NaN]). These are plotted as 5 pairs of bars, one pair per x-axis coordinate. Now, when I try to place text label using the text function, it doesn't seem to know what to do with the (x,y) coordinates I specify in text(x, y, 'string',...).
Presently, I am trying to solve this with the annotation function, so i don't have to address any data-points, but this way is dissatisfactory, as I have to mess with calculating appropriate coordinates for a text-box in each scenario.
Image Analyst
Image Analyst le 25 Sep 2021
What more can I do to help you enough to Accept this answer? Do you have two bars per "coordinate" and want values atop each bar? Post your code if you need any more help.
AR
AR le 27 Sep 2021
Sorry I haven't checked back in a couple of days. Yes, I do have two bars per "coordinate". I am really only concerned with having a text note where the data is NaN - so I can either have two notes for the missing two bars, or just one note at that coordinate (near the y=0 line). Thanks.
Image Analyst
Image Analyst le 27 Sep 2021
So just don't create str and call text if the number is a valid number. Attach your data if you need more help.

Connectez-vous pour commenter.

Produits

Version

R2018a

Tags

Question posée :

AR
le 23 Sep 2021

Commenté :

le 27 Sep 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by