How to read part of a text file as input

1 view (last 30 days)
Kawsar Jahan
Kawsar Jahan on 24 Jun 2015
Edited: Mark Matusevich on 1 Jul 2015
Hi, I am generating some 3D points and saving in a text file. Next I want to take those points as input for another code. here is my text file:
*******OUTPUT 1 for p=0.01 5x5x5********
1 1 4
3 2 4
*******OUTPUT 2 for p=0.01 5x5x5********
2 1 3
1 4 4
*******OUTPUT 3 for p=0.01 5x5x5********
2 1 3
1 4 4
*******OUTPUT 4 for p=0.01 5x5x5********
3 1 5
1 4 4
*******OUTPUT 5 for p=0.01 5x5x5********
3 1 5
1 4 4
For output 1 p=0.01 (1,1,4) and (3,2,4) are my points. I want to read these points as my input 1 when p=0.01. How can I do that?
  3 Comments
Kawsar Jahan
Kawsar Jahan on 24 Jun 2015
I want to match something like below and grab the next line as my input points.
if('OUTPUT %d for p=%2f',k,p)
points = will read the next line
end
Stephen23
Stephen23 on 30 Jun 2015
Edited: Stephen23 on 30 Jun 2015
While it is definitely possible to solve this, it may be many times easier to save the data in a more convenient form, such as in a .mat file, which would avoid the need for awkward textfile parsing.
Currently the problem is not clearly described: all of the matrices in your example data have the header p=0.01, and yet you state that only the first group are of interest. Why only the first group? What is the meaning of 5x5x5? Do the sizes of these matrices change size, does the number of groups change? Please give us more information, and then we can really help you with your code.
Whatever you do please avoid using eval or other hack solutions. There are robust ways of solving this, once we know what you need we can help you know what they are.
Can you please describe your actually code, and what information you are trying to work with. You can upload code using the parpeclip button.

Sign in to comment.

Answers (1)

Mark Matusevich
Mark Matusevich on 30 Jun 2015
Edited: Mark Matusevich on 1 Jul 2015
First, read the file into cell-array of strings by running "fgetl" in a loop, then find the relevant 'OUTPUT %d for p=%2f' line using "regexp" function. Finally, convert the next line into coordinates:
points = eval(['[' lines{ii+1} ']']);
[EDIT -start] Or use the following instead of 'eval':
points = str2num(lines{ii+1});
[EDIT -end]

Categories

Find more on Environment and Settings in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!