The points read with stlread are different from when the same points are saved with stlwrite and read again.

14 views (last 30 days)
As written in the title, after reading the stl file with stlread, I saved the points and connectivityList as variables, then recreated the stl file using the stlwrite function.
Obviously, points and connectivityList used the same data, but the points read from the newly created stl file differed from the previously used variable in the fractional part.
Is there any way to solve these problems?

Answers (1)

akshatsood on 4 Sep 2023
Edited: akshatsood on 4 Sep 2023
Hi Jungwu,
I understand that you are experiencing differences in the fractional part of the coordinates when reading and writing an STL file using stlread and stlwrite functions in MATLAB. As per my understanding, it may be due to the floating-point precision used during the process. This can happen due to the inherent limitations of representing real numbers in computers.
One possible workaround involves using the round function to set the coordinates to desired precision followed by specifying the file format parameter while leveraging the stlwrite function. To demonstrate the workaround, an example script can be opened up by entering the following command in the MATLAB command window
>> openExample('matlab/ReadTriangulationFromSTLTextFileExample')
Adjust the precision variable as per your requirements. By doing this, you can ensure that the fractional part of the coordinates is consistent when reading and writing the STL file. Additionally, you can mention the file format as 'text' while calling stlwrite instead of the default binary to mitigate the differences.
TR = stlread('tristltext.stl');
triangulation with properties:
Points: [6×3 double]
ConnectivityList: [4×3 double]
% set the desired precision for the floating-point numbers
precision = 6;
points = round(TR.Points,precision);
% create a new triangulation object
roundedTR = triangulation(TR.ConnectivityList,points);
% write the rounded data to a new STL file with the file format as 'text'
stlwrite(roundedTR, 'new_file.stl', 'text');
% validate that data is consistent using stlread and stlwrite
newTR = stlread('new_file.stl');
disp(newTR.Points - TR.Points);
I hope this helps.

Community Treasure Hunt

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

Start Hunting!