Effacer les filtres
Effacer les filtres

Reading data with multiple headers

2 vues (au cours des 30 derniers jours)
Byeol Kim
Byeol Kim le 31 Août 2020
Commenté : Byeol Kim le 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 commentaires
Byeol Kim
Byeol Kim le 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 le 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...

Connectez-vous pour commenter.

Réponses (1)

J. Alex Lee
J. Alex Lee le 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 commentaire
Byeol Kim
Byeol Kim le 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!!!

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by