How to make a surface plot of imported data of a numerical function z=f(x,y)
8 views (last 30 days)
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!
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);
If your data are not already in some regular type of grid, let me know and we can discuss that solution.