quiver plot - need help to remove NaN

22 views (last 30 days)
Hi all,
Thanks for your time reading this. I have some trouble to remove NaN values from my quiver plot. I've tried to replace them with zero but it didn't work too.
U(isnan(U))= 0;
Also, I've tried with index and replace it in quiver plot, with this I managed to remove the NaN values but I ended up plotted massive vectors which I don't know how to make them less dense without losing their original information.
idx = ~isnan(U) & ~isnan(V);
I'm not very sure what is a better way to plot them but I prefer not to scale my vector arrows to keep the original information. My script and a screenshot of my plot as follow where you can see black dotes on the land areas (Ireland, the UK and Europe) - this is a result from my SWAN wave model (0.05degree resolution). I want to remove those black dotes. Hope someone can help me. All my variables are in matrix (i.e 461x401) as attached. Sorry, I'm a new Matlab user. Thanks in advance!
colormap (jet);
hold on;
'LineWidth',1,'Color','k') % plot for bigger size and less dense arrows
Thanks, Raydo
Andrew Reibold
Andrew Reibold on 18 Nov 2014
The code you provided produce a different plot for me with the attached data
I solved your issue for the plot below, see in the answers section. Let me know if your problem is resolved.

Sign in to comment.

Accepted Answer

Andrew Reibold
Andrew Reibold on 18 Nov 2014
Edited: Andrew Reibold on 18 Nov 2014
This solution removes the black dots!
The reason it looked awful after removing NaNs is because instead of just taking the non-NaN indices of every 15th point, you took the non-Nan from EVERYTHING which was a TON more data points. even if the arrows were tiny, they still blotted out everything! I first made new variables including only what you wanted plotted, then removed the NaNs from there.
I commented out the quiver line in your original code and added new code below. Your data must be loaded first in the same format as presented in your attachment.
Best wishes! -Andrew
caxis([0,10]); colormap (jet);
hold on;
% quiver(Xp(1:15:end,1:15:end),Yp(1:15:end,1:15:end),...
% U(1:15:end,1:15:end),V(1:15:end,1:15:end),...
% 'LineWidth',1,'Color','k') % plot for bigger size and less dense arrows
Xp2 = Xp(1:15:end,1:15:end);
Yp2 = Yp(1:15:end,1:15:end);
U2 = U(1:15:end,1:15:end);
V2 = V(1:15:end,1:15:end);
idx = ~isnan(U2) & ~isnan(V2);
Andrew Reibold
Andrew Reibold on 18 Nov 2014
No problem. Also, Giorgos mentioned in a comment on the question that "the Code button formats the code part in your answer. It is very is to use this tool. You just highlight the code and you press the button. It improves the readability for someone who reads your question."
I very much agree with his statement. Try to use the code button! :)
(Example is in the solution I posted, its just nicer for us to read, and its clear which parts are the code!)

Sign in to comment.

More Answers (0)


Find more on Vector Fields in Help Center and File Exchange


Community Treasure Hunt

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

Start Hunting!

Translated by