Clear Filters
Clear Filters

How to convert X, Y, Z cartesian coordinates into a surface.

20 views (last 30 days)
Hello,
I have three vectors representing x, y, and z cartesian coordinates of half of a nosecone shape. I need to convert the z data into a "matrix of z heights", as as described in the documentation for Grid Surfaces used in Simscape Multibody (https://www.mathworks.com/help/sm/ref/gridsurface.html#mw_e7b9ffe5-592d-4971-9715-092dba7fd93a).
I tried to use the diag() command and surf() to check things, but it isn't quite right; there are valleys between rows of points and some of the negative space outside the nosecone is also included. Additionally, Simscape grid surfaces need x and y vectors that are monotonic and increasing. I can sort these vectors in Matlab easily enough, I can't seem to get my z matrix quite right when I sort x and y.
Sample code below. The x_coords, y_coords, and z_coords are pulled in from a .mat file.
Thank you!
load('cone_data - Copy.mat');
x = x_coords(:);
y = y_coords(:);
z = z_coords(:);
z = diag(z);
fullset = [x y z];
surf(x, y, z);
  4 Comments
Cris LaPierre
Cris LaPierre on 11 Apr 2024
Please upload the same data you are working with.
The mat file you uploaded does not appear to have the same variables your code uses.
load nosecone.mat
whos
Name Size Bytes Class Attributes ans 1x36 72 char cmdout 1x33 66 char full 675x3 16200 double gdsCacheDir 1x14 28 char gdsCacheFlag 1x1 8 double i 0x0 0 double managers 1x0 0 cell managersMap 0x1 8 containers.Map

Sign in to comment.

Accepted Answer

Voss
Voss on 12 Apr 2024
load('cone_data - Copy.mat');
Here's how the surface looks. This is using the matrices x_coords, y_coords, and z_coords.
% a surface based on x, y, and z matrices
figure
surf(x_coords,y_coords,z_coords)
I understand that for Simscape Multibody you need to specify the surface as a matrix z based on x and y being monotonically increasing vectors, instead of matrices. That constraint is the same as requiring the surface to have a footprint (i.e., its projection on the x-y plane) that is a rectangle. Obviously the surface above does not have a rectangular footprint; it's more like a filled-in parabola kind of shape. In order to define your surface on a rectangular footprint, you'll need to fill in some z values outside the parabola. Using zeros is a natural choice for those values, which can be done something like this:
% need to interpolate x/y/z_coords over some rectangular
% grid, so use a scatteredInterpolant
SI = scatteredInterpolant(x_coords(:),y_coords(:),z_coords(:));
Warning: Duplicate data points have been detected and removed - corresponding values have been averaged.
% define the grid with some equally-spaced x values
x = linspace(min(x_coords(:)),max(x_coords(:)),25);
% and non-equally-spaced y values (because y_coords is already grid-like)
y = y_coords(:,1);
% interpolate to get z on that grid
[X,Y] = meshgrid(x,y);
Z = max(0,SI(X,Y));
% surface based on x, y vectors, and matrix z
figure
surf(x,y,Z);
Another choice might be NaNs (I have no idea what Simscape Multibody will do with NaN z values, but it's worth a shot, I guess), which can be done like this:
% using the same x-y grid as before, but
Z = SI(X,Y);
Z(Z < 0) = NaN;
% surface based on x, y vectors, and matrix z
figure
surf(x,y,Z);
I guess I would try the one with the zeros first.
  4 Comments
Kylen
Kylen on 12 Apr 2024
Thank you! This should be more than adequate for my purposes.
My teammate and I are eternally grateful!
Voss
Voss on 12 Apr 2024
You're welcome! Please "Accept" this answer if it ends up working.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!