How do I read numeric data from a mixed text file?
2 views (last 30 days)
Show older comments
Hi all,
lets say I have a text file that goes like this:
The number of beams
1
The number of columns
2
Location of footing (x and y coordinates)
0 0
10 10
Other data
1 2 100 1000
2 3 100 1000
End of file
I want to find a way to read the numbers from this "mixed" text into these four matrices:
nob (number of beams)
noc (number of columns)
lof (location of footing)
od (other data)
The nob and noc will be 1x1 matrices. As for lof and od, the number of columns is fixed ( 2 for loc and 4 for od) however, there is no limit on the number of rows for loc and od (theoretically).
How can I do that?
Thanks in advance
ShyG
0 Comments
Answers (1)
Walter Roberson
on 8 May 2015
For files like that, often the most practical answer is to use fopen(), a bunch of fgetl() and fscanf(), and fclose() afterwards.
If the file has a slight bit more complexity, then the most practical answer may be to use fopen(), a bunch of textscan() calls with carefully constructed formats and counts, and fclose() afterwards.
If the file is more complex still, sometimes the easiest thing to do is to create a small perl script to process it down to a simpler form that you then read.
For more complex files than that: if there is a regular structure to them then using a tool such as yacc and lex may be the order of the day.
For the irregular complex files, you may need to construct a complicated finite state machine.
See Also
Categories
Find more on String Parsing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!