Plot function behave differently when only using line versus only using markers when plotting data with NaN values

2 views (last 30 days)
Hi,
I am currently trying to plot a matrix,let's call it 'A' containing NaNs in it, when I use plot(x,A, '-k'), it displays correctly (right image below), with gaps when the data is NaN. However, when I try to plot with just markers plot(x,A,'kx') (left image below), there are data points where there should be gaps (where NaN values are), there seems to be some interpolation going on.
I wonder if this is an issue with the Matlab version I have access on: R2021a - if so, is there any work around this? I tried fishing out the indices of non-NaN values and plotting those instead both for x and A variables but there still seems to be some interpolation. Please help.
Many thanks,
Cyrus
  5 Comments
DGM
DGM on 5 Oct 2021
If you really want to remove those isolated points for a particular reason, I suppose you could.
load('sample_data.mat');
m = ~isnan(sample_data);
m = bwareaopen(m,2); % remove runs shorter than 2 samples
sample_data(~m) = NaN;
subplot(2,1,1)
plot(sample_data)
subplot(2,1,2)
plot(sample_data,'o:')
This uses bwareaopen() from IPT, but I'm sure there are other ways. I'm more familiar with image processing tools than signal processing tools, so that's the hammer I'm going to use.
Cyrus Espinoza
Cyrus Espinoza on 5 Oct 2021
Thank you so much for the insight, DGM. I agree, I will have to explore other ways as I do not have the IPT installed.
By the way, you pointing out what I missed is sufficient enough to get me through. I'm aware we are on the comment section so I cannot choose your first suggestion as an 'accepted answer' - are you happy for me to close this question by only acknowledging you here in the comments section? Or would you prefer to re-post your first suggestion as an answer so I can select it as 'accepted answer'?

Sign in to comment.

Accepted Answer

DGM
DGM on 5 Oct 2021
Reposted from comments:
Without knowing what the data looks like, I imagine that not all values are NaN. Markers will be visible for lone points flanked by NaN, whereas a line-only plot shows nothing. Consider the example:
A = rand(1,10);
B = rand(1,10);
B(2:2:end) = NaN; % every other point is NaN
subplot(1,2,1)
plot([A B A])
subplot(1,2,2)
plot([A B A],'o:')

More Answers (1)

Mathieu NOE
Mathieu NOE on 5 Oct 2021
Removing the NaN data will not do any harm on the dot plot, but the solid line will not have the gap anymore
% fix ?
B = A;
B(isnan(A)) = [];
x(isnan(A)) = [];
figure,plot(x,B, '-k')
figure,plot(x,B, 'kx')
  2 Comments
Cyrus Espinoza
Cyrus Espinoza on 5 Oct 2021
Thank you for this suggestion Mathieu, although I was actually trying to achieve a cleaner set of data with the NaNs ignored using just the markers, this will be a useful technique for me in the future if I need to do it this way.

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!