필터 지우기
필터 지우기

Reading data with multiple headers

조회 수: 2 (최근 30일)
Byeol Kim
Byeol Kim 2020년 8월 31일
댓글: Byeol Kim 2020년 9월 8일
I have a huge vtk data that contains multiple dataset (total of 7).
Each dataset is divided by some title in the format of: string number number string.
I identified how to extract the first dataset, but I do not know how to exact the next datasets.
Each dataset represent some 3D coordinate data. For example if there were 1 2 3 4 5 6 in the dataset, that simply means (1,2,3) and (4,5,6). A number before floats in the header represents the number of coordinates in the dataset. For the example I gave in the previous sentence, it should be 2 float.
Can someone help me on how to do this?
I couldn't attach the data since it was too large. Hence, I copied a portion of my data down below.
Data looks like this:
# vtk DataFile Version 2.0
CFD_results
ASCII
DATASET UNSTRUCTURED_GRID
POINTS 3 floats
0.00996074 0.0054144 -0.134876 2.11545e-05 0.0100366 -0.135006 0.00497977 0.00996415 -0.134936
p 1 3 float
-3.65022e-09 -0.000371252 -0.000183657 -8.9538e-05 0.000226161 -0.00031672 -0.000252209 -0.000295019 -0.000275176
wallShearStress 3 3 float
3.1775e-05 2.5129e-05 -3.8414e-05 0 0 0 0 0 0
  댓글 수: 3
Byeol Kim
Byeol Kim 2020년 9월 1일
I read the first data by telling matlab to skip 5 lines and read data from there.
I can't do that with the other datasets because there will be too many lines to skip and these number of lines to skip will vary depends on the vtk file I need to process.
I don't know how to write the code to chop up the file into multiple blocks. That's where I am struggling.
J. Alex Lee
J. Alex Lee 2020년 9월 1일
i made a first pass at an answer, i'm not sure it's the best...but also on another note, it's odd because i thought vtk files are more self-descriptive...maybe that's only for the binary formats where it has to tell you how many bytes make up data blocks, not for ascii formats...

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

답변 (1개)

J. Alex Lee
J. Alex Lee 2020년 9월 1일
other experts on the forum for text parsing may have better ways, but you can scan through line by line and apply regular expression
fid = fopen("vtkfile")
while feof(fid)
linebuffer = fgetl(fid);
hdr = regexp(linebuffer,"\w+ \d+ \d+ \w+","match");
% i'm flying solo with the regexp, i don't know if this pattern will work
% you'll have to look at regexp docs to check
if ~isempty(hdr)
% this is a header
% do something special
% presumably you need to parse the 2 numbers that tell you
% how the following data is formatted or something
else
% this is not a header
% treat the line appropriately
end
end
  댓글 수: 1
Byeol Kim
Byeol Kim 2020년 9월 8일
Your guidance was extremely helpful.
Your code worked but was extremely slow for some reason.
But then I figured out another way that was a lot faster and still accurate.
It involved ~strcmp function.
Thank you!!!

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by