필터 지우기
필터 지우기

Import numbers from header (.txt file)?

조회 수: 3 (최근 30일)
MLP
MLP 2021년 11월 5일
댓글: MLP 2021년 11월 6일
Hello, I have set up a script that will go through many files containing 3D geometries and is essential that I am able to extract the number of vertices and faces listed in the header of each respective file. How can I access thiese numbers (lines 5 and 9 of the attached file) in order to assign them to a variable?
Thank you in advance!
  댓글 수: 2
Walter Roberson
Walter Roberson 2021년 11월 5일
Do you need only those things for a given pass, or will you happening to be reading the data in as well?
If you are reading in the data at the same time, then some of the convenient methods become worth doing, but if you have "many" files and you only need that header information, then the overhead of the convenient methods could be too much and you might need to switch to less convenient but faster methods.
MLP
MLP 2021년 11월 6일
편집: MLP 2021년 11월 6일
I need those two numbers from the header but I also need to import the rest of the file. I am currently using readmatrix() for the later. @Walter Roberson

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

채택된 답변

Walter Roberson
Walter Roberson 2021년 11월 6일
filename = 'testMODEL.txt';
S = fileread(filename);
EV = cell2mat(textscan(S, 'element vertex %f', 1, 'HeaderLines', 4));
EF = cell2mat(textscan(S, 'element face %f', 1, 'HeaderLines', 8));
data = cell2mat(textscan(S, '%f %f %f', 'HeaderLines', 11));
This assumes that those items are always on the same line numbers. If not, then modifications would need to be made to the code.
textscan() of a character string is pretty efficient.
  댓글 수: 1
MLP
MLP 2021년 11월 6일
Thank you so much, this works perfectly!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by