how to extract particular data from .m file
조회 수: 8 (최근 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.
댓글 수: 0
채택된 답변
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
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 Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!