MATLAB: How to find the word for the .dat file and skip the above line from this word

matlab function

I have .dat file and data in this is as shown in image.I want to find the species name. than get the line from fgetl command below this line and line containe this species. how to do this?ex. i want to find H2 from this data and than i want to the line the from H2 name.i also need the line containing H2.How can i do this?

Best Answer

S = fileread('YourFileName.dat');
blocks = regexp(S, '^(?<species>\S+)\s*(?<header>[^\n]*)\n(?<numbers>([^\n]+\n){3})', 'names', 'lineanchors');
Now you have a struct array with fields "species", "header", and "numbers". All three fields will contain character vectors. The numbers fields will not have been interpreted at this point.
You can then
species_names = {blocks.species};
Now you can ismember() the desired species name against species_names to locate the datablock about that species:
relevant_block = blocks(ismember(species_names, target_species));
And now you can access relevant_block.header.
Converting the numbers requires noticing that the fields are fixed widths, to know not to try some things. But you can
sscanf(relevant_block.numbers, '%g', [6 inf]).'