How to structure/sort x,y,z data for surf, mesh, etc types of plots?

55 views (last 30 days)
I'm confused conceptually about how and why my data needs to be organized to make a 3D surface plot, when I start with 3 vectors corresponding to x,y,z positions in a 3D space. Yes, I read the documentation for surf and mesh functions and other threads on similar topics, but I'm still confused.
Currently I have a 7559x3 matrix that I called A. Each row corresponds to a single x,y,z position vector; in other words, each row of A is a single point in 3D space. Simply using surf(A) does not work. The axes scales are not even correct and don't reflect my data range. Using scatter3 does work, but I want a surface plot. Here's what I get from scatter3 command:
I found the follwing comment/answer on a Matlab Central thread on this topic:
Three Nx1 vectors don't define a surface alone, they define a curve. You can use plot3() for that. For a surface you need to specify connectivity in some way, either by organizing the z-data into a rectangular array for surf(), or by coming up with a triangulation for trisurf()
This is what I don't understand: how do you specify connectivity? I'm especially befuddled by the part about a rectangular array. How would the z-data be organized into a rectangular array? How can you possibly make a rectangle out of the z coordinates?? It's just a single list.
  1 Comment
Aristo on 6 Sep 2017
Were you successful in getting what you desired ? If yes could you please share your code

Sign in to comment.

Answers (2)

Talmage Jones
Talmage Jones on 6 Mar 2019
I believe I had a similar problem. I have three 100x1 vectors of data: two vectors of independent variables, l (with values 20,30,40...,90) and d (10,20,30,40,50), and a dependent variable, V. Reading over the documentation for the griddata function, I was able to create a matrix from this data. See script below
[xq,yq] = meshgrid(10:10:90, 10:10:50);
vq = griddata(l,d,V,xq,yq);

Star Strider
Star Strider on 8 Mar 2017
It depends on how your data are organized. If you have your data on a relatively regular grid (regularly repeating values in your x and y vectors), see if griddedInterpolant does what you want. If they are not gridded but scattered, see if scatteredInterpolant works. There are a number of other interpolation functions and options that could also work, and are linked to in those pages.

Community Treasure Hunt

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

Start Hunting!