Hi Nicolás,
You need to add "inpolyhedron" file exchange for checking whether the points are inside a 3D triangulated (faces/vertices) surface. Kindly refer to the below code ofr generating the binary mask.
filename = 'example.STL';
[TR,fileformat,attributes,solidID] = stlread(filename);
addpath('C:\Users\hiteshk\Downloads\inpolyhedron');
% Define the grid resolution and limits
gridSize = 100; % Adjust the resolution as needed
xRange = [min(TR.Points(:,1)), max(TR.Points(:,1))];
yRange = [min(TR.Points(:,2)), max(TR.Points(:,2))];
zRange = [min(TR.Points(:,3)), max(TR.Points(:,3))];
[xGrid, yGrid, zGrid] = ndgrid(...
linspace(xRange(1), xRange(2), gridSize), ...
linspace(yRange(1), yRange(2), gridSize), ...
linspace(zRange(1), zRange(2), gridSize));
% Initialize the binary mask
binaryMask = false(size(xGrid));
% Use inpolyhedron or similar function to fill the binary mask
binaryMask = inpolyhedron(TR.ConnectivityList, TR.Points, [xGrid(:), yGrid(:), zGrid(:)]);
% Reshape the binary mask
binaryMask = reshape(binaryMask, size(xGrid))
You need to "trisurf" function to visualize the STL using the triangulation object. Kindly refer to the below code for visualazing the STL file.
% Create a figure to visualize the STL using the triangulation object
figure;
trisurf(TR, 'FaceColor', 'cyan', 'EdgeColor', 'none');
title('STL Visualization');
xlabel('X');
ylabel('Y');
zlabel('Z');
grid on;
axis equal;
view(45, 25);
camlight('headlight');
lighting gouraud;

For more information regarding the "inpolyhedron" and "stlread", kindly refer to the below MATLAB documentation: