- 1. select the variable you want to replace
- 2. Press control + f (this is used to find). A pop out comes, there will be a option find, replace,
- replace all.
- 3. Type what you want to replace with..
- 4. Go for find and replace or Replace all.
(MATLAB TRICK AND SHORTCUTS): I would like to rename all my variables within a script without do it manually one by one
923 vues (au cours des 30 derniers jours)
I have a function with inputs (a,b,c,d) and I would like to have the same function in a vector x such that a=x(1), b=x(2), c=x(3), d=x(4) without rename the one by one.
I am asking that because when I underline a variable such "a" all the "a's" within the code highlight as well. I was thinking that there might be a command to change all of them instantaneously into x(1). The same for the other variable
PS: I possess the version of matlab (R2016a)
KSSV le 7 Avr 2017
Modifié(e) : KSSV le 7 Avr 2017
One of the option I follow:
Be careful while replacing.
Plus de réponses (3)
Image Analyst le 13 Avr 2017
Simply click in the variable that you want to rename, on a line where you assign something to it. Start editing the name and you'll see a popup tooltip string says "Press shift-Enter to rename n instances.....". So do that, type shift enter and all your variables will be renamed to the current/new name that you just gave it.
Jan le 7 Avr 2017
Modifié(e) : Jan le 7 Avr 2017
Rename the variable at first to a unqiue name by editing one instance and hitting - I do not remember, perhaps Shift-Return (it should appear as a context menu or tooltip). Rename "a" to "a_replace_", if this string does never occur inside the function.
Afterwards your can perform a standard Replace from the FIND menu (Ctrl-f): "a_replace_" to "x(1)" - see KSSV's answer.
dbmn le 7 Avr 2017
There is a simple solution for this, although it is ugly. But it takes minimal time to adapt.
function [out] = myfun(a, b, c, d)
function [out] = myfun(x)
a = x(1); b=x(2); c=x(3); d=x(4);
I agree with the other posters, using single letters for variables might not be the best thing to do. I do this in loops but otherwise usually put my variables in structs that you can handle quite easy.