MATLAB Answers

How to make a surface plot of imported data of a numerical function z=f(x,y)

5 views (last 30 days)
Daniel Bozi
Daniel Bozi on 5 May 2021
Commented: Chad Greene on 5 May 2021
I'm completely new to MatLab and I might not understand the coding ecosystem that MatLab is built upon. What I'm trying to do is really simple in my mind, but I still don't have the know-how of MatLab to do it efficiently.
Here is my issue: I have a python code that calculates a number z that takes two variables x and y, so z = f(x,y). The x-y plane is a rectangle divided up into many equally spaced points, and z would then be the height over each point when plotted. Accordingly, the data is arranged according to equally spaced coordinates x and y, followed by the corresponding z value. Assume there are N x-values and M y-values, each x-y pair is listed on the same row with its corresponding z-value, so something like:
x1 y1 z11
x1 y2 z12
x1 yM z1M
x2 y1 z21
x2 y2 z22
x2 yM z2M
and so on until the final x-coordinate:
xN y1 zN1
xN y2 zN2
xN yM zNM
So, it's essentially a file with NxM rows and 3 columns, with each number separated by a space.
How do I make a surface plot out of this data, where each (xi,yj) is a point on the x-y plane and the corresponding zij is the height above that point?
I feel a little lost even after consulting the help pages, since none of those seem to apply to my case. Particularly, it seems I don't know how to make the surf command know how to interpred the first two columns as coordinate pairs row-by-row, over which the third column is the corresponding height. Maybe surf or surface are not the right ways to think about this problem?
Any help on this would be greatly appreaciated!
  1 Comment
Daniel Bozi
Daniel Bozi on 5 May 2021
Interestingly, the surf command returns a figure which is entirely non-sensical, where the only numbers I recognise are the ones on the right hand side of the picture below, which seems to enumerate the rows 1-3456 for each of the 3456 rows of my file.

Sign in to comment.

Answers (1)

Chad Greene
Chad Greene on 5 May 2021
Just to be clear, you already have the data, and it takes this form
M = [x y z];
where x, y, and z are columns?
My next question is, are x and y spaced in some type of grid, like
M =[1 1 5;
1 2 3;
1 3 7;
2 1 9;
2 2 1;
2 3 2;
3 1 2;
3 2 9;
3 3 8]
What I'm asking is, do the first to columns have some kind of repeating pattern that creates a regular grid of points x,y? If so, an easy solution is to use my xyz file functions. Check out the examples on that page. Syntax would be
[X,Y,Z] = xyz2grid(M(:,1),M(:,2),M(:,3);
shading interp
If your data are not already in some regular type of grid, let me know and we can discuss that solution.
Chad Greene
Chad Greene on 5 May 2021
Oh, perfect. It looks like x and y are on regular intervals of 0.05, so it might even be able to do everything in one step:
[X,Y,Z] = xyzread('expo-largeu.dat');
shading interp

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!