MATLAB Answers

Reading data with multiple headers

1 view (last 30 days)
Byeol Kim
Byeol Kim on 31 Aug 2020
Commented: Byeol Kim on 8 Sep 2020
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 Comments

J. Alex Lee
J. Alex Lee on 1 Sep 2020
how did you find and extract the first data set (wallShearStress)? It seems to me if you are able to do that, you can apply the same logic to the remaining ones...
you could parse in 2 steps, e.g., scan through the file and identify your headers to chop up the file first into blocks based on splitting at the headers, then apply your within-header parsing for each block.
Byeol Kim
Byeol Kim on 1 Sep 2020
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 on 1 Sep 2020
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...

Sign in to comment.

Answers (1)

J. Alex Lee
J. Alex Lee on 1 Sep 2020
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 Comment

Byeol Kim
Byeol Kim on 8 Sep 2020
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!!!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!