Matlab 3D Plot (surf) fills in empty gaps automatically

19 Ansichten (letzte 30 Tage)
JSL
JSL am 3 Apr. 2013
I'm using surf() to plot a portion of a sphere; basically a shell of a certain shape that is on top of a sphere. More specifically, it is the workspace plot of spherical joint with color representing the resolution of the joint at that point.
Anyways, the shape is unique, but surf() automatically fills in gaps that should not be there. I'm guess it might be the order that it is plotted? Is there any way to prevent Matlab from doing this?
I think it is obvious in the pictures what should not be there (the blue portion)...
Also, it seems to mess up the color coding as well. The dark blue should be at the bottom edges of the shell; instead Matlab assigns it to the surface used to fill the gaps.
Thanks!

Antworten (2)

Walter Roberson
Walter Roberson am 3 Apr. 2013
How are you telling surf() that data should not be there?
The results you are showing are consistent with you having initialized a matrix to 60, writing data into part of it, and then surf()'ing the whole thing.
  2 Kommentare
JSL
JSL am 3 Apr. 2013
Sorry, I forgot to mention that...and that is most likely where my problem lies now that I think about it.
Basically, I created a square grid, then ran each point through my code. For points in Z(x,y) that should not be there (ie not possible to reach), they are given a value of NaN. The corresponding x and y values are also given a NaN value. So, in the end I have a sparse matrix of values with NaNs filling up everything else.
Do I need to reshape each matrix and remove all the NaNs?
JSL
JSL am 4 Apr. 2013
I still quite haven't figured out how to get this to look right.
My last attempt was to take all my matrices and extract each individual 3D point (removing all the NaN), leaving me with 3 vectors of (x,y,z) points. Then using griddata() to interpolate the 3D data (scatter) set. The surface comes out, but it looks pretty messy as the interpolation isn't very clean.
Any ideas?
Does surf() only work well if the grid is square? In my case, I started with a spherical coordinate grid (az and el), then ended up converting to Cartesian coordinates for plotting.

Melden Sie sich an, um zu kommentieren.


Omid Adljuy
Omid Adljuy am 5 Apr. 2013
Set the Z data that you don't want to be plotted to NaN.
But let the X,Y data remain what they are; I mean don't set X,Y to NaN.
This shall work.
  1 Kommentar
JSL
JSL am 5 Apr. 2013
Thank you for the response! I believe I had my code like that in the beginning, but ended up switching it. It does plot the surface without the 'filler' surface, so that is good, but the problem still is that the color coding is still off. The minimum z value plotted should be the darkest blue, but it still looks like the darkest blue is assigned to the 'filler' surface that is no longer there:
*Ignore all the text...not sure why it did that. I'm using an older version of Matlab at the moment and never had this issue with the newer version I regularly use.
So, the plot looks better, but the colors are off. If I didn't care about the colors, then I would be on my way, but unfortunately I need them to be correct.
I tried something else that seems to get me a good plot. I'm using a matrix C as the color scaling (eg surf(X,Y,Z,C) ), so I tried keeping X, Y, and Z untouched (no NaN), but leaving the NaN in C. Doing that gives me this plot:
You can see the colors are correct, now. Then, setting the shading to flat removes all the lines:
This gets me a good looking plot, finally. From the looks of it, my very first plot in this post is missing the smallest z-value surface, which is where my dark blue color should be. And the grid size looks to be a little different, even though the resolution in the code never changed.
Anyways, I think that will be good enough for the plots I have to generate. If you have any further insight, I'd love to hear it!
Thanks again!

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by