how to extract particular data from .m file

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.

 Accepted Answer

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 Comments

Note that the only way to find your macros robustly is to actually parse the file. This is not something that a regular expression can do.
Matlab actually includes a parser for m files that you can use in the form of mtree. It is unfortunately not documented. The following is slightly more robust than a pure regex as mtree gets rid of the comments:
filetree = mtree('active_configuration.m', '-file');
str = filetree.strings;
macroNames = regexp(str,'(?<=#define\s+)\w+', 'match');
macroNames = [macroNames{:}]
includeNames = regexp(str, '(?<=#include\s+")[A-Za-z_0-9.]+(?=")', 'match');
includeNames = [includeNames{:}]
N/A
N/A on 20 Jul 2015
Edited: N/A on 20 Jul 2015
Thanks you very much @Guillaume for comments and answers..
Is it recommended to use undocumented mtree in code?
Guillaume
Guillaume on 20 Jul 2015
Edited: Guillaume on 20 Jul 2015
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.
Thank you..

Sign in to comment.

More Answers (0)

Categories

Products

Asked:

N/A
on 20 Jul 2015

Edited:

on 20 Jul 2015

Community Treasure Hunt

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

Start Hunting!