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.

 채택된 답변

Friedrich
Friedrich 2011년 8월 15일

0 개 추천

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

Steffan
Steffan 2011년 8월 15일
thank you, I will try it and accept the answer if it works :)
Steffan
Steffan 2011년 8월 16일
works like a charm, thx
Walter Roberson
Walter Roberson 2011년 8월 16일
I would suggest using dynamic field names rather than eval.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Text Data Preparation에 대해 자세히 알아보기

질문:

2011년 8월 15일

댓글:

2021년 11월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by