Fscanf help ( text file handling )

5 visualizaciones (últimos 30 días)
Steffan
Steffan el 15 de Ag. de 2011
Comentada: nimeesh rathi el 1 de Nov. de 2021
Hello I'm having trouble figuring out how to get information from a .txt file. The file would be structured as follows:
[Headline1]
variable1
variable2
variable3
[Headline2]
variable4
variable5
What I need to be able to, is to create a struct with fields names as the Headlines, and each field must contain the associated variables.
I'm currently trying with fscanf.
  1 comentario
nimeesh rathi
nimeesh rathi el 1 de Nov. de 2021
can you please upload your code ?

Iniciar sesión para comentar.

Respuesta aceptada

Friedrich
Friedrich el 15 de Ag. de 2011
Hi,
I would use fgetl to read a full line. Than I would check if [ ] exists in that line, e.g. with strfind. If so, remove the [ ] and create a new struct field with this name, e.g. use the eval command.
If the line doesn't include [ ] than you have to assign this variable to the field.
  3 comentarios
Steffan
Steffan el 16 de Ag. de 2011
works like a charm, thx
Walter Roberson
Walter Roberson el 16 de Ag. de 2011
I would suggest using dynamic field names rather than eval.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Text Data Preparation en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by