필터 지우기
필터 지우기

General parsing function

조회 수: 2 (최근 30일)
maxroucool
maxroucool 2012년 5월 4일
Hi all,
I would like to parse a txt file in order to import it into Matlab so it is much more easier to treat.
The format of the txt file is kind of homemade, it looks like json but it doesn't seem to be so.
This is how it looks:
generator : "PAM-RTM 2011.0";
format_version : 2011.0;
date : "Thu May 03 15:04:21 2012";
General_Parameters {
simulation_description : "";
sampling_period : 10;
recover_sampling_period : 500;
max_experiment_time : 1.000000E+006;
fixed_dt_max : 1.000000E+025;
}
material {
name : "Default Resin";
family : material;
type : resin;
density : 1.000000E+003;
nb_vars_specific_heat : 0;
specific_heat {
family : function;
name : noname;
}
}
Of course I could do it with regexp() function, but I would like something robust. So I am looking for a function in which I could say what are the separating characters (such as {} : ;) and that generates a Matlab structure. Does it exists?
BR,
Max
  댓글 수: 2
Jan
Jan 2012년 5월 4일
As long as you do not define the wanted output format, an answer must contain a lot of guessing.
Daniel Shub
Daniel Shub 2012년 5월 4일
What makes you think that doing it with regexp is going to be robust? Or is it that you are worried that it is not going to be robust if you write it? It doesn't seem to me to be that hard of a question.

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

답변 (2개)

Jan
Jan 2012년 5월 4일
No, due to the high level of generality, I'm sure there is no such function.

maxroucool
maxroucool 2012년 5월 4일
Thanks for your answers.
It's not that i don't trust regexp but rather that i don't trust my regexp skills. Anyway i will try it myself!
Br,
  댓글 수: 3
maxroucool
maxroucool 2012년 5월 4일
So what would you suggest without using regexp?
Walter Roberson
Walter Roberson 2012년 5월 4일
What *I* would suggest would be to write a BNF grammar that matches your needs, and implement it in yacc and lex. That will produce C code, that you can write a mex wrapper for.
If you do not already know yacc and lex, I would suggest to you that it would be worth your time studying them: not necessarily for the syntax, but because they deal with fundamental text parsing concepts that you need to reproduce in whatever implementation you use.

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

카테고리

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