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

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

Can you upload the mat file, using the paperclip button?
Hi, thanks for asking. I added a .mat file. It's the x, y, z data in three columns. My original .mat was given to me unsorted (many columns for each x, y, z), so I uploaded the version that's a bit less cumbersome.
Anyway, my attachment is x, y, z. The z column is giving me trouble. I think I might need to interpolate between points to get a surface. I've tried using diag() to kind of fill in the "empty" spots with zeros. That was helpful, but not quite right.
If you hadn't guessed, this is a little outside of my wheelhouse, so I appreciate the help.
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

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

Hello, and thank you so much! This is the closest I've gotten so far. I think your first version with the zeros will work for my purposes. I'll be contact modeling on the top contours of the curve, so the "flat" underside shouldn't really come into play.
Do you happen to have a suggestion for the following Simscape error?
  • ['untitled/Grid Surface']: The number of elements in the X Grid Vector parameter must equal the number of rows in the Z Heights matrix parameter. Resolve this issue in order to simulate the model.
  • ['untitled/Grid Surface']: The number of elements in the Y Grid Vector parameter must equal the number of columns in the Z Heights matrix parameter. Resolve this issue in order to simulate the model.
I tried to take transpose x and y such that the dimensions match that of the Z matrix, but that didn't seem to work either.
Again, thanks for helping!
You're welcome!
Transpose Z or use ndgrid rather than meshgrid to get rid of those errors.
Double-checking:
load('cone_data - Copy.mat');
% 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);
Z is numel(y)-by-numel(x) because that's convenient for plotting a surface
% interpolate to get z on that grid
[X,Y] = meshgrid(x,y);
Z = max(0,SI(X,Y));
whos x y Z
Name Size Bytes Class Attributes Z 27x25 5400 double x 1x25 200 double y 27x1 216 double
But Simscape Multibody expects Z to be numel(x)-by-numel(y)
% interpolate to get z on that grid
[X,Y] = ndgrid(x,y);
Z = max(0,SI(X,Y));
whos x y Z
Name Size Bytes Class Attributes Z 25x27 5400 double x 1x25 200 double y 27x1 216 double
Thank you! This should be more than adequate for my purposes.
My teammate and I are eternally grateful!
You're welcome! Please "Accept" this answer if it ends up working.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 11 Apr 2024

Commented:

on 12 Apr 2024

Community Treasure Hunt

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

Start Hunting!