How to change all variable names in a Matlab function?
Afficher commentaires plus anciens
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 commentaires
Stephen23
le 24 Sep 2015
What is the aim of this? A use-case description would be helpful.
Benjamin
le 24 Sep 2015
Why is it required to "merge" these functions together? Are the functions identical?
It is not clear why this "merge" might be desirable: most programming best-practice recommends encapsulation of functionality, and the use of functions for code that needs to be repeated. Separate, smaller functions are easier to write, easier to bug-fix, and much easier to test. Merging them into one large file does not seem to have any particular advantages, yet has the disadvantage that the code now will have "indexed" variable names, which are meaningless and poor programming practice.
Benjamin
le 24 Sep 2015
One file is a totally different thing to having multiple functions: MATLAB actually has no (practical) restriction on how many functions can be saved in one Mfile:
Why not just add those functions as local functions in the same Mfile?
Réponse acceptée
Plus de réponses (2)
Guillaume
le 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 commentaires
Walter Roberson
le 24 Sep 2015
There are a lot of builtin functions that people like to override with variables. Especially "sum", and "image"
Jan
le 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
le 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.
Catégories
En savoir plus sur Variables dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!