Borrar filtros
Borrar filtros

How to read part of a text file as input

5 visualizaciones (últimos 30 días)
Kawsar Jahan
Kawsar Jahan el 24 de Jun. de 2015
Editada: Mark Matusevich el 1 de Jul. de 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 comentarios
Kawsar Jahan
Kawsar Jahan el 24 de Jun. de 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 el 30 de Jun. de 2015
Editada: Stephen23 el 30 de Jun. de 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.

Iniciar sesión para comentar.

Respuestas (1)

Mark Matusevich
Mark Matusevich el 30 de Jun. de 2015
Editada: Mark Matusevich el 1 de Jul. de 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]

Categorías

Más información sobre String Parsing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by