how to extract particular data from .m file

5 次查看(过去 30 天)
hello,
For example, I have code in file like below.
cs.set_param('SimCustomHeaderCode', sprintf('%s\n%s','#define A1 10','#define RESLOLUTION 0.25')); % Header file
cs.set_param('CustomHeaderCode', sprintf('%s\n%s','#define A2 10','#define RESLOLUTION_1 0.25')); % Header file
cs.set_param('CustomSourceCode', '#include "verify_includes.c"'); % Source file
I want to extract CustomHeaderCode in one array and CustomSourceCode names in other array.I tried to do using regexp like below:
str = fileread('active_configuration.m');
[macroNames Index] = regexp(str,'(?<=\#define)\s+(\w+)','match','start')
but it gives me unwanted names also.
I am expecting answer as A1,RESLOLUTION,A2 and RESLOLUTION_1 in one array and verify_includes in another array.

采纳的回答

Guillaume
Guillaume 2015-7-20
The problem with your regex is that it also picks up some of the comments that also include #define. A simple way to solve this with the example file you've posted is to ensure that the defined macro starts with an uppercase character.
[macroNames, Index] = regexp(str,'(?<=#define\s+)[A-Z]\w*', 'match','start')
%I've also moved the \s+ into the look behind as I assume you don't want the spaces in macroNames
%there's no need to escape #
%there's also need to enclose the required match in ()
With the attached file, this regex should work for the include:
[includeNames, Index] = regexp(str, '(?<=#include\s+")[A-Za-z_0-9.]+(?=")', 'match', 'start')
  4 个评论
Guillaume
Guillaume 2015-7-20
编辑:Guillaume 2015-7-20
That is up to you. It's been part of matlab for years and as far as I can tell has not changed.
Furthermore, Mathworks routinely makes breaking changes to documented functions so the line between undocumented but stable and documented functions is very thin.
If you do use mtree, then comment the code appropriately to explain that it's undocumented and may break in future versions.
On arbitrary m-files, the mtree version is always going to be more robust than regular expressions alone. If you control the m-file generation so that comments and string contents can't break your regular expressions, then regex alone would be suitable.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by