Using if statement in analyzing/editing a txt file

I am trying to develop a code to analyze a text file in MATLAB. The text file was an stl file for 3D printing. It repeats 7 lines about 100000 times to call out each triangle in the stl file. I am trying to develop a code using sscanf, fprintf, and if statements to cut away the bottom portion of the file for printing. I know the code will need to analyze the z values, cut them out if they are below the halfway point, and put the points back into a modified txt/stl file. How can I do this? Here's an example of the first 7 lines. I was thinking to use an if statement that if the line begins with 'vertex' it would analyze the 3rd number value in the row?
facet normal 0.278814 -0.935724 -0.216066
outer loop
vertex -0.596372 -4.060564 5.681240
vertex -0.555372 -4.055394 5.711758
vertex -0.599424 -4.075450 5.741772
endloop
endfacet

7 Comments

Your instructions are not very clear. What is the expected output for the snippet you provided? Is it
5.681240
5.711758
5.741772
?
I attached a text file for what I am trying to do. Basically I am trying to develop a code that analyzes z values and will cut them off if they are below a certain threshold. Ideally the bottom half of the image. I gave those 7 lines as an example because the text file repeats 7 lines to describe each triangle.
"I attached a text file..."
Not in sight, unfortunately...
Sorry, I thought it uploaded. Its there now.
Update. I am trying to get only the lines with 'vertex' first then write them to another text file. Heres the code I have that isn't working right.
filename= 'Thinker.txt'
file_identifier=fopen(filename)
if file_identifier==-1
disp(['Couldn''t find this file', filename])
error('File does not exsist')
end
this_line=0;
all={};
while this_line~=-1
this_line==fgetl(file_identifier)
if this_line==startsWith(lines, 'Vertex')
all=[all;this_line];
end
end
Try:
data = fileread('Thinker.txt');
[matches]=regexp(data,'(?=vertex)(.*?)(?=vertex|endloop)','match');
It only pulls out the x value instead of x y and z how can I fix that

Sign in to comment.

Answers (0)

Asked:

on 21 Jul 2018

Commented:

on 21 Jul 2018

Community Treasure Hunt

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

Start Hunting!