How to read part of a text file as input
2 次查看(过去 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 个评论
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
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]
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 String Parsing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!