How to change all variable names in a Matlab function?
38 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I would like to have a method for duplicating a function and at the same time rename all variables in the file so that they have a specific prefix, for example:
function c = main(m)
a = m*2
b = m*3
c = a+b+m;
should be duplicated and the prefix 'main_' should be added. The new function becomes:
function main_c = main(main_m)
main_a = main_m*2
main_b = main_m*3
main_c = main_a+main_b+main_m;
Thanks for any inputs :)
5 Kommentare
Akzeptierte Antwort
Jan
am 24 Sep. 2015
Bearbeitet: Jan
am 24 Sep. 2015
You need a powerful parser, which can distinguish names of variables from (shadowed) functions, strings and longer names, which contains the string as a part only.
Most likely somebody has written such a thing already and published it in the FEX. What about < http://www.mathworks.com/matlabcentral/fileexchange/15924-farg--a-pedestrian-m-file-parser-showing-all-used-functions--syntax> It should contain all you need. But the modification of the code to replace the strings is not trivial.
I guess that solving the problem in the editor is much faster than creating a powerful parser as a program. Using an automatic tool is only useful, if the codes are large and complicated. And especially in this case, finding potential bugs of the parsing+replacing procedure is very hard. I expect, that the required time for validating and debugging will exceed the advantage in runtime massively.
The efficiency of a program is determined by the time required to solve the problem, not by the runtime only.
Weitere Antworten (2)
Guillaume
am 24 Sep. 2015
If you want your code to be able to automatically detect the variables in a file, you're going to need some syntax parser. There is actually one shipped with matlab, mtree, but it is completely undocumented so you'll have your work cut out. mtree may also completely change or be removed in future version as it's not supported by mathworks.
Anyway, the following will give you the list of variable names and function names in a file. There may be a way to differentiate between variable and functions but you'll have to work that out yourself:
parsetree = mtree('main.m', '-file');
treeids = parsetree.mtfind('Kind', 'ID'); %ID are all variables and functions used by the file
idstrings = unique(treeids.strings);
isbuiltin = cellfun(@(id) exist(id, 'builtin') == 5, idstrings); %an easy way to at least remove matlab built-in functions from the list
nonbuiltinid = idstrings(~isbuiltin)
Once you've got the list of variables to replace, you can use fileread to read the whole file at once and strrep or regexprep to perform the replacement.
However, neither a, b, c or main_a, main_b, main_c are good variable names. None of them convey the purpose of the variable.
Furthermore, if the code is properly written, there should be some comments / documentation stating the purpose of each named variables, so you would also have to edit the documentation.
3 Kommentare
Jan
am 24 Sep. 2015
Replacing the string in the file directly would affect quotes strings also, and longer names, which contain the replaced part. In ths OP's case changing "c" to "main_c" would destroy "function".
Guillaume
am 24 Sep. 2015
Indeed, my suggestion of using strrep or regexp was a bit flawed. On the other hand, the mtree parser can also give you the position of the tokens. In my example above:
treeids(~isbuiltin).position
With regards to variables shadowing built-in functions, I went with the assumption that someone capable enough to understand and manipulate the output of mtree has enough experience with matlab to know not to do this sort of things.
Siehe auch
Kategorien
Mehr zu Characters and Strings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!