how to extract particular data from .m file
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposta accettata
Guillaume
il 20 Lug 2015
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 Commenti
Guillaume
il 20 Lug 2015
Modificato: Guillaume
il 20 Lug 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.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Characters and Strings in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!