How to read part of a text file as input

1 次查看(过去 30 天)
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 个评论
Kawsar Jahan
Kawsar Jahan 2015-6-24
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 2015-6-30
编辑:Stephen23 2015-6-30
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.

请先登录,再进行评论。

回答(1 个)

Mark Matusevich
Mark Matusevich 2015-6-30
编辑:Mark Matusevich 2015-7-1
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]

类别

Help CenterFile Exchange 中查找有关 String Parsing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by