How can I create a 3D solid body from a surface plot (x y z data points) and export it (preferably as STL) to a CAD software?

19 views (last 30 days)
I have an x y z dataset and I want to create a solid 3D body (almost as if 'extruding' the surface down in a CAD software) from the topmost 3D surface. I am trying to achieve something similar to this
.
Having tried that, I am getting the following error :
Error using stlwrite (line 25)
Input argument must be a triangulation object.
Error in surface_to_3D (line 22)
stlwrite('test3D.stl',test3D);
If anyone could help me solve this, or tell me an alternative way to achieve this, that would be very helpful. All required files that I have used are attached.
P.S. The reason I cannot do it in a CAD software is because Solidworks or any other software cannot really read/detect the surface for extrusion, even if I use surf2stl to export it as an STL file.

Answers (2)

Walter Roberson
Walter Roberson on 26 Jul 2022
There are file exchange contributions for surf2stl() and patch2stil()
  3 Comments

Sign in to comment.


DGM
DGM on 30 Jun 2025
Well, nobody caught this one. OP was likely working off of examples that used stlwrite() from FEX #20922, but since R2018b, MATLAB has a built-in STL tool stlwrite(). Their usage isn't identical, so either change your usage to suit the tool you're using, or go get the tool you're trying to use.
% Generate x y z data
H = 0.8; % hurst exp
sigma = 100; % rms roughness
Lx = 1000; % length
m = 50; % number of pixels - x
n = 50; % number of pixels - y
z = artificial_surf(sigma, H, Lx, m , n);
x = linspace(0,Lx,m);
y = linspace(0,Lx*n/m,n);
[X,Y] = meshgrid(x,y);
surf(X,Y,z,'edgecolor','none'); %% plot surface
% Convert surface to solid
test3D = surf2solid(X,Y,z,'elevation',min(z(:))-500);
% write the file using FEX #20922
%stlWrite('test3D.stl',test3D) % i renamed this to avoid collision
% or write the file using the built-in writer
T = triangulation(test3D.faces,test3D.vertices);
stlwrite(T,'test3D.stl')
% read it back for show and tell
Tr = stlread('test3D.stl');
patch('faces',Tr.ConnectivityList,'vertices',Tr.Points,'facecolor','w','edgecolor','none');
view(3); view(144,44); camlight;
axis equal; grid on
xlabel('X'); ylabel('Y'); zlabel('Z')

Products

Community Treasure Hunt

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

Start Hunting!