General parsing function
2 次查看(过去 30 天)
显示 更早的评论
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
2012-5-4
As long as you do not define the wanted output format, an answer must contain a lot of guessing.
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 个)
maxroucool
2012-5-4
3 个评论
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 Center 和 File Exchange 中查找有关 String Parsing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!