Want to plot a mesh structure in matlab but in live script this error comes up.

조회 수: 2 (최근 30일)
Shivam Dave
Shivam Dave 2021년 4월 21일
편집: DGM 2025년 7월 17일
This is the code section
stlFile = 'Lift_Arm_New_2.stl';
figure
trisurf(stlread(stlFile))
axis equal
Getting this error:
Index exceeds the number of array elements (0).
Error in trisurf (line 78)
x = varargin{1};

답변 (2개)

Chidvi Modala
Chidvi Modala 2021년 6월 3일
The triangulation object returned from stlread(stlFile) might be empty. Hence the error.
You may verify this by executing the following code
stlFile = 'Lift_Arm_New_2.stl';
TR = stlread(stlFile);
And check if the workspace variable TR is empty.
  댓글 수: 1
DGM
DGM 2025년 7월 17일
편집: DGM 2025년 7월 17일
The error is not caused by an empty triangulation object, because the input to trisurf() is not a triangulation object. That error can only occur if the first argument is not a TriRep or triangulation object, and it refers to varargin being empty -- which follows the first argument. That means trisurf() is looking for a second argument and there isn't one.
Even if they had a triangulation object (which they didn't), I don't see how it could have been empty. I'd like to see a plausible way to unwittingly create an empty triangulation object.
The built-in stlread() (as far as I've managed to test it) will not return an empty triangulation for an empty STL. It returns an error. Given the way an STL is constructed, stlread()'s requirement for 3 vertices necessitates the inclusion of (at minimum) 1 face. The triangulation cannot be empty.
% an STL with no facet blocks
T = stlread('nothing.stl')
Error using stlread (line 52)
Input file must contain at least 3 unique vertices.
Error in AT_sandbox (line 19)
T = stlread('nothing.stl')
You can't directly create an empty triangulation object. The least you can have is a single degenerate face and three vertices. Since the object is read-only, you can't edit a valid triangulation to make it empty.
% try to create an empty triangulation object
T = triangulation(zeros(0,3),zeros(0,3)) % 0F, 0V
Error using triangulation
At least three input points must be provided to define a triangular mesh.
Error in AT_sandbox (line 19)
T = triangulation([1 2 3],zeros(0,3))
% well the error only says we need three points, so ...
T = triangulation(zeros(0,3),rand(3,3)) % 0F, 3V
Error using triangulation
The input triangulation is empty.
Error in AT_sandbox (line 28)
T = triangulation(zeros(0,3),rand(3,3))
% try to create a minimal triangulation object
% one face and three vertices are required
% the one face does not have to be non-degenerate
% but you still need no fewer than 3 vertices
T = triangulation([1 1 1],rand(3,3)) % 1F, 3V
T =
triangulation with properties:
Points: [3×3 double]
ConnectivityList: [1 1 1]
Maybe it can be done, but it's not immediately obvious to me. We know from their code that they were not doing something so esoteric anyway.

댓글을 달려면 로그인하십시오.


DGM
DGM 2025년 7월 16일
편집: DGM 2025년 7월 17일
This error is happening because OP found some old or anachronistic advice on the forum, so they downloaded the STL decoder stlread() from FEX #22409 or any one of a handful of others with the exact same name.
Since R2018b, MATLAB has come with STL tools stlread and stlwrite. If you're running a modern version, you already have them. You don't need to download old, incomplete decoders that have the same name with incompatible behavior and functionality.
When called with <2 output arguments, #22409 will return an FV struct. When given a single input argument, trisurf() interprets it as a triangulation object -- at least that's what the documentation suggests. In reality, it can be either a TriRep object, a triangulation object, or if it's neither of the two, trisurf simply assumes that the input is a face list followed by x,y,z vectors. Consequently, it tries to retrieve varargin{1:3} (the second through fourth input arguments), which results in an indexing error into varargin, not into the input data itself. We can see that from the error message. This is the behavior in R2019b:
Index exceeds the number of array elements (0).
Error in trisurf (line 78)
x = varargin{1};
Apparently, this input check has been fixed sometime more recently, as the error is now informative. This is in R2024b:
trisurf(stlread_2011('stepholecube.stl')) % i renamed the function for obvious reasons
Error using assert
Specify a triangle connectivity matrix followed by x, y, and z co-ordinates, or a triangulation or delaunayTriangulation object.

Error in trisurf (line 40)
assert(numel(varargin)>=3 || isa(tri, 'TriRep') || isa(tri, 'triangulation'), ...
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

카테고리

Help CenterFile Exchange에서 STL (STereoLithography)에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by