Programmatically get edge ids from geometry of femodel

I am using matlab's pde toolbox and setup a geometry for femodel like this:
gd = [3 4 0 1 1 0 0 0 1 1]'; % Example: A simple square
sf = 'S1';
ns = ('S1')';
[g,bt] = decsg(gd, sf, ns)
fe = femodel(AnalysisType="electrostatic", Geometry=g);
fe.PlanarType = "axisymmetric";
figure(1); clf;
pdegplot(fe, 'EdgeLabels', 'on', 'FaceLabels','on');
axis equal off;
I can get a diagram indicating which edge-id is assign to an edge:
The resulting geometry does not contain information, which vertices form the edges:
>> fe.Geometry
ans =
fegeometry with properties:
NumFaces: 1
NumEdges: 4
NumVertices: 4
NumCells: 0
Vertices: [4×2 double]
Mesh: []
How can I retrieve which edge id belongs to which vertices in the geometry.
How can I retrieve which edge id is where programmatically?How can I retrieve which edge id is where programmatically?

 Accepted Answer

% Source - https://stackoverflow.com/q/79888928
% Posted by MiB_Coder
% Retrieved 2026-02-16, License - CC BY-SA 4.0
gd = [3 4 0 1 1 0 0 0 1 1]'; % Example: A simple square
sf = 'S1';
ns = ('S1')';
[g,bt] = decsg(gd, sf, ns);
fe = femodel(AnalysisType="electrostatic", Geometry=g);
fe.PlanarType = "axisymmetric";
figure(1); clf;
pdegplot(fe, 'EdgeLabels', 'on', 'FaceLabels','on')
axis off
hAx=gca;
hE=findobj(hAx,'Type','Text') % find the edge label objects to see the tag values
hE =
5×1 Text array: Text (PDESubLabel) Text (PDEEdgeLabel) Text (PDEEdgeLabel) Text (PDEEdgeLabel) Text (PDEEdgeLabel)
hE=findobj(hE,'Tag','PDEEdgeLabel') % find which are the edges
hE =
4×1 Text array: Text (PDEEdgeLabel) Text (PDEEdgeLabel) Text (PDEEdgeLabel) Text (PDEEdgeLabel)
e=arrayfun(@(h)str2num(extractAfter(h.String,'E')),hE,'uni',1) % get the edge numbers
e = 4×1
4 3 2 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Note: Don't need two steps above once know what the tag value is are looking for; can call findobj on axis directly.
xyz=reshape ([hE.Position],3,[]).'; % retrieve the position (3D coordinates)
xy=xyz(:,1:2) % only need x,y here
xy = 4×2
0 0.5000 0.5000 1.0000 1.0000 0.5000 0.5000 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
tE=array2table([e xy],'VariableNames',{'Num','X','Y'})
tE = 4×3 table
Num X Y ___ ___ ___ 4 0 0.5 3 0.5 1 2 1 0.5 1 0.5 0
EDGES='LRBT'; % look up for edge name/id
ix=find(tE.X==fix(tE.X)); iy=find(tE.Y==fix(tE.Y)); % find the edge coordinate locations
edge=[EDGES(tE.X(ix)+1) EDGES(tE.Y(iy)+3)].'; % get the corresponding letter
edge=reshape(edge([ix iy]),[],1); % reorder and covert to column vector
tE.Edge=edge % add to the table
tE = 4×4 table
Num X Y Edge ___ ___ ___ ____ 4 0 0.5 L 3 0.5 1 T 2 1 0.5 R 1 0.5 0 B
Compare the coordinates of the text labels to find which is on which...in general it won't be quite so simple in that one will have to match up coordinates that aren't just integers so will have to match the x and y coordinates of each element to the end coordinates of the elements in both x and y. I've not tried to generate a test case, but I'd expect one would be able to use an inverse lookup in interp to do the search.
ADDENDUM:
Figured there must be some more tools -- look at faceEdges; I suspect you may be able to do something with it. Not having the PDE TB, it's inconvenient to try to do much exploring in this environment...

1 Comment

Indeed it works, also for more complex geometries.
With help of tech support, i figured out a similar approach, using nearestEdge(), which can be used to find the edge id closest to a point (e.g. the midpoint of an edge as you did).

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!