Why doesn't the subtract function work with 3D DiscreteGeometry in R2024b?
Show older comments
% Why doesn't the subtract function work with 3D DiscreteGeometry in R2024b?
model = femodel(); % Create a Finite element analysis model object
% Define dimensions of the rectangular plate (width, height, thickness)
plateWidth = 10;
plateHeight = 6;
plateThickness = 1;
% Create the rectangular plate
rectPlate = multicuboid(plateWidth, plateHeight, plateThickness);
% Define dimensions of the circular hole (radius, height)
holeRadius = 1;
holeHeight = plateThickness;
% Create the cylinder for the hole
cylinderHole = multicylinder(holeRadius, holeHeight);
% Subtract the cylinder from the plate
plateWithHole = subtract(rectPlate, cylinderHole);
% The error message follows...
'subtract' is used in the following examples:
-----------------------------------------------------------
I have also tried to use:
plateWithHole = addVoid(rectPlate, cylinderHole);
and I get the following error message.
Added cells must be located strictly inside a single cell of the original geometry.
In the error-generating case, the cylindrical void is the same thickness as the rectangular plate. If I make the cylindrical void ever so slightly shorter than the thickness of the plate, it works without error, but then I don't get a hole that goes completely through the plate.
-----------------------------------------------------------
What other Boolean functions will work with these DiscreteGeometry types that result in a hole going completely through the flat plate?


-----------------------------------------------------------
I’ve heard it said that subtract works in R2023, but I don't want to revert to that version.
Thank you,
Tim
3 Comments
The only subtract() function/method I can find is the one for 2D polyshape objects. I wouldn't expect it to work with other objects.
which subtract -all
I don't see that DiscreteGeometry objects have any subtract method, but I don't have PDE toolbox, so I'm not going to go digging under the hood.
That said, I have no idea what the error message is supposed to mean. When I run the same code, it doesn't give links to anything. None of the lines in the error message yield any useful search results (but most things don't these days). I'm probably missing something, but this isn't a very helpful error message.
% Subtract the cylinder from the plate
plateWithHole = subtract(rectPlate, cylinderHole);
Paul
on 1 Jan 2025
If there was a "subtract" method for DiscreteGeometry objects in R2023 and there isn't now, perhaps that decision is discussed in the release notes for whatever version removed that method, perhaps pointing to an alternative method.
Don't know why the error message doesn't come with a link. But it looks like if "subtract" isn't found then Matlab does search through the documentation to find potential use-cases. I've never seen this type of message before (I've always seen "function not found" or something like that), perhaps it's a new feature.
I'm not sure, but the lack of links in the error message might just be a forum-only thing. I don't have the PDE toolbox, so I don't know.
Certainly, I found a lot of function() related docs links, but as much as those are relevant to function(), none are relevant to subtract().
Accepted Answer
More Answers (1)
Walter Roberson
on 1 Jan 2025
0 votes
In R2023a, the methods available for DiscreteGeometry are
addCell addVertex cellEdges extrude facesAttachedToEdges nearestFace scale
addFace addVoid cellFaces faceEdges nearestEdge rotate translate
You can subtract() certain kinds of Antenna Toolbox shapes; https://www.mathworks.com/matlabcentral/answers/2092471-creating-solid-geometry-with-pde or some RF Toolbox shapes
For subtracting geometries with the PDE toolbox, you use decsg
10 Comments
But why isn't the shape.Box method found by which -all?
which subtract -all
box = shape.Box("Height",10 ,"Length",30,"Color","r","Width",10);
which subtract -all
Why did we have to instantiate a Shape.Box for its subtract method to be found? Is that because the method is implemented in Shape.m and not subtract.m (as for polyshape)? Or maybe it has something to do with Shape.m being in a namespace? If so, does the same apply for ordinary m-functions in a namespace, i.e., an m-function in a namespace won't be fround by which until after its been used, or maybe not until after something in the namespace has been used?
Timothy
on 2 Jan 2025
"Unfortunately, it doesn't work in R2024b Update 3."
It seems to work on this forum:
% Define the start and end points
xStart = -13;
xEnd = 13;
% Number of cylinders to create
numCylinders = 5;
% Calculate the spacing between each cylinder
spacing = (xEnd - xStart) / (numCylinders - 1);
% Initialize an array to hold the Cylinder objects
cylinders = cell(1, numCylinders);
% Loop to create each Cylinder and set its center
for i = 1:numCylinders
xCenter = xStart + (i-1) * spacing;
cylinders{i} = shape.Cylinder("Radius", 1, "Height", 10, ...
"Color", [0.9290, 0.6940, 0.1250], ...
"Center", [xCenter, 0, 0]);
end
box = shape.Box("Height",10 ,"Length",30,"Color","r","Width",10);
show(box)
for i = 1:numCylinders
box = subtract(box, cylinders{i});
end
show(box)
which subtract(box, cylinders{i});
Walter Roberson
on 2 Jan 2025
Note that this subtract() is of Antenna Toolbox shapes, not of DiscreteGeometry
Walter Roberson
on 2 Jan 2025
The example is by @Stephen23
% Why doesn't the subtract function work with 3D DiscreteGeometry in R2024b?
model = femodel(); % Create a Finite element analysis model object
% Define dimensions of the rectangular plate (width, height, thickness)
plateWidth = 10;
plateHeight = 6;
plateThickness = 1;
% Create the rectangular plate
rectPlate = multicuboid(plateWidth, plateHeight, plateThickness);
% Define dimensions of the circular hole (radius, height)
holeRadius = 1;
holeHeight = plateThickness;
% Create the cylinder for the hole
cylinderHole = multicylinder(holeRadius, holeHeight);
% Subtract the cylinder from the plate
whos rectPlate cylinderHole
methods(rectPlate)
rectPlate is a DiscreteGeometry. subtract() is not defined for DiscreteGeometry. subtract() is defined for shape.* objects -- which are objects from the Antenna Toolbox, not from the PDE Toolbox.
I'm the tangential simpleton here, but decsg() sounds terribly obtuse for object-level CSG, especially considering that I wouldn't have found out about it even after an hour's worth of dredging the docs for a toolbox I don't have. I'm going to re-suggest a comment I deleted earlier.
Is it possible or practical with your expected workflow to develop the composite geometry as a mesh and then convert it to a DiscreteGeometry object afterwards? I have seen that GPToolbox (FEX) has some CSG tools which I haven't tested, and there are any number of external tools that could do this. Even external tools can be exploited programmatically via system().
I don't have PDE toolbox and don't use DiscreteGeometry for anything, so I don't know if this is helpful. I'm just throwing this out there, since none of this seems like it's a satisfying resolution to the problem.
Timothy
on 2 Jan 2025
Categories
Find more on Geometry and Mesh in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!





