Finding value behind string

3 views (last 30 days)
Sebastian
Sebastian on 12 Apr 2012
Hello everybody,
I'd like to screen a text-dokument (with numeric values and character-strings in a header section which is repeated every N rows; and numeric values in form of a matrix underneath the header) and collect all values after a certain string. The value stands periodically (alone) in the next line of a certain string.
How is it possible to collect all these values in an array?
Thanks a lot in advance, kind regards
Sebastian
  1 Comment
Sebastian
Sebastian on 12 Apr 2012
Sorry,
I think I should have described the problem clearer:
the textdokument looks like this:
ITEM: TIMESTEP
1
ITEM: NUMBER OF ATOMS
1000
ITEM: BOX BOUNDS
-1 1
-1 1
-0.1 2
[1......
2......
.......
999....
1000...]
ITEM: TIMESTEP
2
ITEM: NUMBER OF ATOMS
1005
ITEM: BOX BOUNDS
-1 1
-1 1
-0.1 2
[1......
2......
.......
1004...
1005...]
...
and so on...
I'd like to extract the number of atoms within different timesteps, which means:
I want to create an array, which stores all the values, that follow the string "ITEM: NUMBER OF ATOMS" in the text document (in the example it's the values 1000 and 1005).
Thanks very much for your help!
regards
Sebastian

Sign in to comment.

Accepted Answer

Richard
Richard on 12 Apr 2012
Couldn't you import all of the text file into matlab using textscan and then select the appropriate fields afterwards? It may prove to be easier to deal with the document once it is in matlab rather than trying to select the required fields directly from the text file.
textscan:

More Answers (1)

Image Analyst
Image Analyst on 12 Apr 2012
How about using fread(), fgetl(), and fclose()? Open the file, call fgetl() a number of times to read and throw away the header, then read the line you want and extract the value. You could use fscanf(), textscan(), str2double() and maybe some other ways. Can you give an example of the line of text that contains the value(s) you want to extract?

Community Treasure Hunt

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

Start Hunting!

Translated by