Sibbs Gambling
Sibbs Gambling

Reputation: 20355

Ignore NaN in MATLAB plotting?

Suppose I have

A = [1 2 3 nan 5];

If I do

plot(1:5, A, 'o-');

I will have the blue part as below.

enter image description here

How to achieve the red part?

Update

I am sorry for not making the point straight in the first shot, but the isnan() method that helps skip those values is not desired, because I need to plot many of those lines, some of whom have missing values (NaN) at some random locations. So I have to keep the x-axis consistent for every line. That is why I cannot simply skip NaN.

Upvotes: 2

Views: 9352

Answers (2)

Abdulbasith
Abdulbasith

Reputation: 149

x = linspace(1,10,10);
y = [1 2 3 nan 5 6 7 nan 9 10];
figure, plot(x,interp1(x,y,x,'spline'))

Upvotes: 0

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272667

Not super elegant, but functional:

idxs = ~isnan(A);
x = 1:5;
plot(x(idxs), A(idxs));

Upvotes: 11

Related Questions