MATLAB Answers

List variables in m-file

15 views (last 30 days)
I would like to rename some variables in an m-file, and would like to automate this process. I have tried using regexprep, but ran into some problems. I noticed a feature in the newest versions of Matlab, where you can automatically rename variables. Is there a command line version of this functionality. Alternatively, what would be the best way of renaming the variables, without having to run the file?

  2 Comments

Cedric Wannaz
Cedric Wannaz on 25 Apr 2013
Do you know the name of these variables a priori, or is it something that has to be detected by the program?
Etienne  Coetzee
Etienne Coetzee on 25 Apr 2013
Yes, I have the names, but some variables are for instance N and NN, so I am not sure how to prevent NN from changing when I want to change N to AB for instance. NN will then become ABAB.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 24 Apr 2013
Example of replacing a variable in a string:
S = 'x^2 + 3*x*y + x1/x';
regexprep(S, '(?<![A-Za-z0-9_])x(?![A-Za-z0-9_])', 'YO')

  3 Comments

Cedric Wannaz
Cedric Wannaz on 25 Apr 2013
Is there a reason why you didn't use \w in the look-around? Was it just to make the pattern more explicit for the example?
Etienne  Coetzee
Etienne Coetzee on 25 Apr 2013
Hi Walter. That is exactly what I wanted. I have tried it, and it works. Thank you. That has made my day.
Walter Roberson
Walter Roberson on 25 Apr 2013
\w would likely be a good idea. I was thinking at the time that it matched a slightly different set of characters than was needed for MATLAB identifiers, but you are right that it is the same.

Sign in to comment.

More Answers (0)

Sign in to answer this question.

Products