File Exchange Pick of the Week

November 2nd, 2007

Advanced MATLAB: Surface plot of nonuniform data

Two minute video shows how to fit a surface to nonuniform data.
Real data is not always on a nice X,Y grid, but the MATLAB commands
SURF and MESH expect them to be. GRIDDATA and MESHGRID help to fix this.

Video Content

Find the files here.

PodCast here

Other videos have been gathered here:
http://blogs.mathworks.com/pick/category/video/

13 Responses to “Advanced MATLAB: Surface plot of nonuniform data”

  1. Masatoshi KASUGA replied on :

    Dear Sir:

    I am very grad to find the program of “advanced-matlab-surface-plot-of-nonuniform-data”, but I could not run it
    because of no-coincide of matrix dimension.
    Would you please inform me of a mistake I coded (copied).

    Best Regards,
    ————————

    %3D graph for nonuniform_data
    x=rand(100,1)*16-8;
    y=rand(100,1)*16-8;
    r=sqrt(x.^2+y.^2)+eps;
    z=sin(r)./r;
    %
    xlin=linspace(min(x),max(x),33);
    ylin=linspace(min(y),max(y),33);
    [X,Y]=meshgrid(xlin,ylin);
    Z=griddata(x,y,z,X,Y,’cubic’);
    %
    mesh(X,Y,Z); % interpolated
    axis tight; hold on
    plot3(x,y,z,’.',’MarkerSize’,15)
    %%surf(X,Y,Z)

  2. paul replied on :

    I copied the code from you message and it worked perfectly on my side! - sorry!

  3. Doug replied on :

    The code you pasted here worked exactly right for me too. I am not sure what to do for you.

    Sorry,
    Doug

  4. Doug replied on :

    Greg pointed me to this solution he wrote a while back that might be of interest also:

    Tech Note 1212 - Using MESHGRID and GRIDDATA to Fit Vector Data and Plot Unevenly Spaced Data http://www.mathworks.com/support/tech-notes/1200/1212.html

  5. Els replied on :

    Dear Doug,

    This info about making a surface plot was very usefull for me. In stead of the formule that you used to produce some points, I used 4 XYZ-coordinates that result in an surface in 3D. Is there an easy way to calculate the surface area of this created object?

    Best regards,
    Els

  6. Doug replied on :

    Els,

    There is no built in function to do this, but knowing the X,Y,Z of each point in the mesh, you should be able to calculate the area of each four point patch. From there, summing should be easy to know the whole area.

    Doug

  7. Jim replied on :

    I have three column data (time,height,collection) supposedly x,y,z data. I want to plot it and get the equation. How can I do that with Matlab.
    eg data:
    x - y - z
    1 - 1 - 130
    2 - 1 - 130
    3 - 1 - 130
    4 - 1 - 130
    7 - 2 - 130
    10 - 2 - 130
    16 - 2 - 130
    25 - 2 - 130
    40 - 2 - 130
    63 - 3 - 130
    100 - 3 - 130
    160 - 3 - 130
    250 - 3 - 130
    300 - 3 - 130
    400 - 3 - 130
    500 - 3 - 130
    1 - 3 - 260
    2 - 3 - 260
    3 - 3 - 260
    4 - 3 - 260
    7 - 3 - 260
    10 - 3 - 260
    16 - 4 - 260
    25 - 4 - 260
    40 - 4 - 260
    63 - 6 - 260
    100 - 8 - 260
    160 - 10 - 260
    250 - 11 - 260
    300 - 11 - 260
    400 - 11 - 260
    500 - 11 - 260
    1 - 4 - 390
    2 - 5 - 390
    3 - 5 - 390
    4 - 5 - 390
    7 - 6 - 390
    10 - 7 - 390
    16 - 8 - 390
    25 - 10 - 390
    40 - 12 - 390
    63 - 14 - 390
    100 - 15 - 390
    160 - 19 - 390
    250 - 22 - 390
    300 - 26 - 390
    400 - 30 - 390
    500 - 30 - 390
    1 - 4 - 650
    2 - 5 - 650
    3 - 5 - 650
    4 - 5 - 650
    7 - 5 - 650
    10 - 7 - 650
    16 - 8 - 650
    25 - 9 - 650
    40 - 12 - 650
    63 - 14 - 650
    100 - 18 - 650
    160 - 22 - 650
    250 - 27 - 650
    300 - 27 - 650
    400 - 36 - 650
    500 - 40 - 650
    1 - 2 - 910
    2 - 2 - 910
    3 - 2 - 910
    4 - 3 - 910
    7 - 5 - 910
    10 - 5 - 910
    16 - 7 - 910
    25 - 9 - 910
    40 - 15 - 910
    63 - 20 - 910
    100 - 24 - 910
    160 - 32 - 910
    250 - 38 - 910
    300 - 41 - 910
    400 - 46 - 910
    500 - 49 - 910

  8. venkat replied on :

    I have multiple [x,y,z] matrices of my data with which i want to plot the surface using a 7th order polynomial, how can I do that??

  9. Doug replied on :

    Venkat,

    I am not sure that I understand the question. If you have x,y,z triples, you just plot them. Are you trying to fit a polynomial to them? That would imply a two dimensional dataset. Are you trying to fit some kind of a surface to the data?

    You might be best off looking here:

    http://blogs.mathworks.com/pick/how-to-get-help/

    It shows how to ask for help at the bottom.

    Doug

  10. Daniel replied on :

    Hi Doug

    Thanks for these videos, they have really helped me alot. I am currently looking to create a surface plot (non uniform data) but I would like to represent it in the z-axis. To explain myself better:

    If you think of various layers of semi circles made up of dots, and as you move ‘up’, the number of dots are increasing, so it ends up looking like a funnel. Now the code you presented here creates a surface for each circle (ie. for each layer), but now I want to create a surface joining the layers. I hope I have explained myself adequately. Thanks again and I would really appreciate your input.

    Kind Regards

  11. Doug replied on :

    @Daniel,

    I think you would be well served to look at the series about volume visualization. It starts here:

    http://blogs.mathworks.com/videos/2009/10/23/basics-volume-visualization-19-defining-scalar-and-vector-fields/

    The one about isosurfaces will be posted today or tomorrow (11/10/2009). That is very likely the function you seek.

    Doug

  12. Teja replied on :

    Dear Doug
    Is there anyway to plot a surface from nonuniform data without meshgrid and griddata? Basically i have topographic information, where the altitude(z) at each x and y is known.
    I do not want any interpolation in between. x and y do not form a rectangular grid but are closely spaced.
    Thanks

  13. Doug replied on :

    @Teja,

    I think this will work:

    http://www.mathworks.com/access/helpdesk/help/techdoc/ref/delaunaytriclass.html

    Doug

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Bob, Brett & Jiro share their favorite user-contributed submissions from the File Exchange.

  • Zach: Hi Doug and Les, I didn’t have a lot of time to mess with this, but I did find a work-around. I plotted...
  • hamed: k
  • Les: @Zach This isn’t exactly what you are looking for but at least it puts all three parameters on the same...
  • Zach: Thanks for your suggestions Doug. I’ll give that a shot and see what happens. I’ve seen many of...
  • Doug: @Zach, I would say to use plotYYY, because that is close to what you want, but using depth as Y makes sense....
  • Doug: @Teja, I think this will work: http://www.mathworks .com/access/helpdesk /help/techdoc/ref...
  • Gify: merry christmas :) nice christmas tree! Regards, Janet Gify
  • Teja: Dear Doug Is there anyway to plot a surface from nonuniform data without meshgrid and griddata? Basically i...
  • Zach: I’m working with geophysical data, so I’d like to produce a depth profile. The y-axis would be...
  • Doug: @Ashok First, please do not use variable names that are MATLAB commands (std and mean). Second, p(j) should be...

These postings are the author's and don't necessarily represent the opinions of The MathWorks.