MATLAB: Create volume out of two surfaces

stlsurfacevolume

I'm trying to create a volume out of two surfaces for a parameter study. The volume should also be exportable as stl-file. The code for the surfaces is shown below.
% Points in xy-plane
nx = 100;
px = linspace(1,50,nx);
pyi = 5+sin(0.8*px);% inside

py = 6+sin(0.8*px);% outside

% rotation x-axis
n=25;
t=(0:n)'*0.5*pi/n;
figure;
surf(ones(n+1,1)*px,cos(t)*pyi,sin(t)*pyi);% inside
hold on
surf(ones(n+1,1)*px,cos(t)*py,sin(t)*py);% outside
I tried to apply the code from Filling area between two planes in 3d plot but I'm not able to make it work. If I use the code below, I get the error report that the indices on the left side are not compatible with the size of the right side at the line v3(r,c,s) = min(d1,d2);
%%Create 3D grid containing distance to closest surface
[x,y,z] = ndgrid(linspace(0,50,nx),linspace(0,50,nx),linspace(0,50,nx));
v = zeros(size(z));
for r=1:n+1
for c=1:n+1
for s=1:n+1
d1 = z(r,c,s) - sin(t)*pyi(r,c);
d2 = sin(t)*py(r,c) - z(r,c,s);
if d1 < 0
v3(r,c,s) = d1;
elseif d2 < 0
v3(r,c,s) = d2;
else
v3(r,c,s) = min(d1,d2);
end
end
end
end
%%Create isosurface
figure
p = [patch(isosurface(y,x,z,v3,0)), ...
patch(isocaps(y,x,z,v3,0))];
isonormals(y,x,z,v3,p(1))
set(p,'FaceColor','yellow')
set(p,'EdgeColor','none')
set(p,'FaceLighting','gouraud')
view(3)
camlight right
I'd be very grateful for your help.

Best Answer

Tetra mesh generation. Let you do exportation in STL file
% Points in xy-plane
nx = 100;
px = linspace(1,50,nx);
pyi = 5+sin(0.8*px);% inside
py = 6+sin(0.8*px);% outside
% rotation x-axis
n=25;
t=(0:n)'*0.5*pi/n;
XYZI = cat(3,ones(n+1,1)*px, cos(t)*pyi, sin(t)*pyi);
XYZE = cat(3,ones(n+1,1)*px, cos(t)*py, sin(t)*py);
isbdri = false(size(XYZI,1),size(XYZI,2));
isbdri([1 end],:) = true;
isbdri(:,[1 end]) = true;
isbdre = false(size(XYZE,1),size(XYZE,2));
isbdre([1 end],:) = true;
isbdre(:,[1 end]) = true;
XYZI = reshape(XYZI,[],3);
n = size(XYZI,1);
XYZE = reshape(XYZE,[],3);
XYZ = [XYZI; XYZE];
isbdr = [isbdri; isbdre];
T = delaunay(XYZ);
b = T<=n;
keep = any(b,2) & any(~b,2) & sum(isbdr(T),2)<=3;
T = T(keep,:);
% This will take long time
close all
tetramesh(T,XYZ,'FaceColor','g')