how to use surf function?

42 views (last 30 days)
Muazma Ali
Muazma Ali on 29 Jul 2022
Commented: Steven Lord on 15 Nov 2023
Hi!
I found on the internet that it is posssible to use surf instead of using gscatter, but I am wondering whether it can take in single column matrix as a the 'z' argument.
I am experiencing problems when I am converting my one column array to a single column matrix for so passing it to surf.

Answers (1)

John D'Errico
John D'Errico on 29 Jul 2022
Edited: John D'Errico on 29 Jul 2022
Almost always, when someone says they have a single vector of z values and they want to use surf, they mean that they have a list of points, scattered in nature, that they believe falls on some surface. A surface here is intended to describe a 2-dimensional curved shape. And when these individuals say this, they want MATLAB to somehow know how to connect those points into a surface.
SORRY. Surf cannot do that. SURF expects an array of points. Think of it as if you generate a grid, using a tool like meshgrid in the (x,y) plane. Now evaluate some function z(x,y). That is what surf can plot. It needs to know how the points are connected together. If all you will give it are a set of points, but then give it no hint as to how things are connected together? Then SURF will fail you. That is called scattered data.
How can you solve it? Typically, one is forced to use a tool for interpolation that can handle scattered data (scattereInterpolant is a good choice here.) You pass a grid of points, created using meshgrid, through the 2-dimensional interpolation tool. That generated a nice regular gridded surface, and surf is now happy and able to work.
Another option is to use a tool like my gridfit, as found on the file exchange. It uses approxaimtion methods to try to find a surface that passes through the set of scattered points you provide. Again, the result, if your data is any good and not too noisy, will be a nice regular surface.
In both of the above cases, if your data happens to NOT follow a nice, regular, single valued relationship, then you need to use something else. For example, a set of points that lie on the surface of a sphere will cause these tools to fail, since they presume a single-valued relationship.
  2 Comments
VIGNESH BALAJI
VIGNESH BALAJI on 15 Nov 2023
@John D'Errico Thanks a lot for the nice explanation. I am using surf to fit a plane between 2 points and it is failing me stating that z value of surf needs to be a matrix and it does not take a 2*1 matrix.
I believe it is because of the same reason you provided above. Please let me know other ways to fit a plane between 2 points. I am able to do affine fit between these 2 points to get the plane's normal and centre point, I am unsure how to plot a plane using this information alone without using a surf function.
I am showing the code for your reference.
p_0 = [1.25, 1.0, 1.0];
p_1 = [1.0, 1.0, 1.0];
plane_1_points = [p_1; p_0];
figure;
XYZ_1 = plane_1_points;
plot3(XYZ_1(:,1),XYZ_1(:,2),XYZ_1(:,3),'r.');
hold on
%compute the normal to the plane and a point that belongs to the plane
[n_1,~,p_1] = affine_fit(XYZ_1);
%plot the two points p_1 and p_2
plot3(p_1(1),p_1(2),p_1(3),'ro','markersize',15,'markerfacecolor','red');
%plot the normal vector
quiver3(p_1(1),p_1(2),p_1(3),n_1(1)/3,n_1(2)/3,n_1(3)/3,'r','linewidth',2)
%plot the two adjusted planes
%[X,Y] = meshgrid(linspace(0,1,N));
X = reshape(XYZ_1(:,1), [2,1]);
Y = reshape(XYZ_1(:,2), [2,1]);
%first plane
surf(X,Y, - (n_1(1)/n_1(3)*X+n_1(2)/n_1(3)*Y-dot(n_1,p_1)/n_1(3)),'facecolor','red','facealpha',0.5);
Steven Lord
Steven Lord on 15 Nov 2023
I am using surf to fit a plane between 2 points and it is failing me stating that z value of surf needs to be a matrix and it does not take a 2*1 matrix.
Two points define a line. There are an infinite number of planes that contain that line. Without a third point not on that line, which one of those infinite number of planes do you want to draw and why did you choose that particular plane?
As a simple example, consider the X axis as the line. On that line the Y and Z coordinates are all 0. Do you want to choose the X-Y plane (the plane where the Z coordinate is 0 but X and Y can take on any value) or the X-Z plane (the Y coordinate is 0 but X and Z can take on any value) as "the" plane containing that line? [There are many others that you could choose, but let's keep this example simple.]

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!