Help with using scanf and editing a txt file?

I'm trying to modify an stl file to eventually 3D print. I am trying to take out the bottom half of the object by using matlab. Essentially, I converted the stl file to .txt so I can analyze the z values to cut away the bottom half. I am trying to use the scanf function or something like it to read the vertices of the triangles, find max and min of xyz, and delete triangles if z<(zmax+zmin)/2. How can I accomplish this?

6 Comments

This can be done..it depends on how your file is...show us your file...
Heres the zip file, its a pretty large file...
Guillaume
Guillaume on 20 Jul 2018
Edited: Guillaume on 20 Jul 2018
You're going to struggle with sscanf, I'd use one or more regex personally. I presume it's a whole facet you'd want to remove if your condition is met, everything from facet normal ... to endfacet ?
I did not understand the condition you want to use to eliminate facets. Can you be clearer (e.g. with a concrete numerical example)?
edit: Actually, the first thing you should do is search the FileExchange to see if there isn't an stl reader and writer. Assuming it has been decently written, it should be a lot more reliable than writing your own parser.
Yes that's correct I want to eliminate the whole facet (or triangle). I basically I want to look at the z values and cut away the bottom half, sorry if there was confusion on that. I was thinking Id scan the txt file, analyze the z values, and delete the ones that correspond to the lower half, then resave the new file. Am I on the right track? I know with sscanf I can analyze line by line but I was thinking I could analyze every 7 lines with a loop to accomplish that. Is that feasible?
Assuming that we have code to read the vertices (it's not too hard to write that efficiently) that produces for example a Nfacet x Nvertices x 3 matrix. How do you decide which facets to remove? Mathematically.
I'm pretty new to matlab and programming so I am still working on a code to read the vertices. Do you have any guidance towards that?

Sign in to comment.

 Accepted Answer

Guillaume
Guillaume on 20 Jul 2018
Edited: Guillaume on 20 Jul 2018
This is how I'd read all the vertices.
%assumptions:
% all vertices are made of 3 coordinates
% all faces are made of 3 vertices
%the code completely ignore the non-vertex lines
lines = strsplit(fileread('thinker.txt'), \n'); %read whole file and split into lines
vertlines = strjoin(lines(startsWith(lines, 'vertex')), '\n'); %identify vertex lines and rejoin
vertices = reshape(sscanf(vertlines, 'vertex %f%f%f\n'), 3, 3, []); %read vertices coordinates and reshape into something useful
This will give you a 3 x Nvertex x Nfacet matrix.
As you've not explained what filtering you want to do after that, I can't help you further.

1 Comment

Thank you. So should I come up with a max allowable z value to input to filter out all the others. Essentially I am just trying to cut away the bottom half of the image and I know that will be done through the z values. So would I come up with a range of acceptable z values?

Sign in to comment.

More Answers (0)

Asked:

on 20 Jul 2018

Edited:

on 20 Jul 2018

Community Treasure Hunt

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

Start Hunting!