fegeometry telling my I have infs or nans when I do not
Show older comments
I am trying to create an fegeometry object from a mesh loaded with readSurfaceMesh. The problem is that MATLAB thinks there are infs or nans when it tells me there are none. What could be the issue?

1 Comment
Answers (1)
You already checked most of the following possible issues. Checking for very large/small values instead of NaN/Inf, checking for self-intersections of the geometry and checking for empty rows might be worth trying.
That's what AI returns:
When fegeometry in MATLAB reports that inputs contain Inf or NaN (Not-a-Number) despite a visual check of your data, it usually indicates that the numerical values are present within the data structure but are hidden, poorly scaled, or of an incompatible data type.
Here are the most common causes and solutions for this false positive, according to MathWorks and community discussions:
1. Hidden NaN/Inf in Complex Geometries (Most Common)
Even if your vertices look fine, a single vertex or STL face normal containing a NaN or Inf will trigger this error.
- Action: Explicitly check for non-finite values in your vertex matrix V and connectivity list F:
MATLAB:
if any(~isfinite(V(:)))
warning(
'Non-finite values found in vertices!');
end
- Solution: Use fillmissing or find to remove or replace these rows.
2. STL Import Issues (If using fegeometry('file.stl'))
If importing an STL file, the issue often lies in corrupt vertex data or invalid facet normals within the file itself.
- Action: Try repairing the STL using stlread to load the data first, repairing it, and then passing it to fegeometry.
MATLAB
tr = stlread('filename.stl');
V = tr.Points;
% Check for NaNs and fix
V(isnan(V)) = 0;
tr = triangulation(tr.ConnectivityList, V);
model = fegeometry(tr);
3. Data Type Issues (Unsigned Integers)
If your input numerical data is in an unsigned integer format (e.g., uint32), it may not align with fegeometry's requirement for double precision.
- Solution: Cast all vertex data to double:
MATLAB:
vertices = double(vertices);
4. Poorly Scaled or Extremely Large Values
Values exceeding the maximum capacity of double-precision floating-point numbers (roughly 1.8e+/-308) can cause operations to result in Inf.
- Action: Check the magnitude of your coordinates. If they are extremely small or large, rescale your geometry.
5. Self-Intersecting Geometry
If your geometry has self-intersections or non-manifold edges, fegeometry can fail, sometimes misreporting the error as NaN.
- Solution: Set the AllowSelfIntersections option to true to check if the error changes.
MATLAB:
model = fegeometry(tr, 'AllowSelfIntersections', true);
6. Empty Rows in Input
Sometimes, a data file (like a CSV used to create vertices) may have trailing commas or empty lines, which are interpreted as NaN or 0.
- Action: Inspect the last few lines of your matrix in the Variable Editor for NaN values.
Summary Checklist
- Run any(isnan(V(:))) and any(isinf(V(:))) on your vertex list.
- Cast all data to double.
- Ensure STL files are valid (not broken/non-manifold).
- Remove rows with non-finite values.
Categories
Find more on Geometry and Mesh in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!