フィルターのクリア

(MATLAB TRICK AND SHORTCUTS): I would like to rename all my variables within a script without do it manually one by one

337 ビュー (過去 30 日間)
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)
  2 件のコメント
Guillaume
Guillaume 2017 年 4 月 7 日
Well, the moral of this is not to use one letter variable names (particularly for function inputs).
A good variable names should convey the purpose/content of the variable to somebody who doesn't know the code. a, b, c, d mean absolutely nothing at all to any reader.
enzo fiasco
enzo fiasco 2017 年 4 月 13 日
I see what you mean but my question was more related on how to use matlab more efficiently. My question could have been: -how to change my input named "sigma" to "mean" without doing by hand with the guarantee it is done in every section of my script? -
Anyway, thanks for the suggestion because I was not doing it consistently in my codes and it is better to correct my mistakes and form a good habit sooner than later.

サインインしてコメントする。

採用された回答

KSSV
KSSV 2017 年 4 月 7 日
編集済み: KSSV 2017 年 4 月 7 日
One of the option I follow:
  • 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.
Be careful while replacing.

その他の回答 (3 件)

Image Analyst
Image Analyst 2017 年 4 月 13 日
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.
  3 件のコメント
Lina Koronfel
Lina Koronfel 2022 年 3 月 29 日
is there a way to rename a variable only within a specific section of the code? @Image Analyst
Image Analyst
Image Analyst 2022 年 3 月 29 日
@Lina Koronfel, are you saying the shift enter way is not working for you? If not you'll have to use control-h to replace. Unfortunately there is no way with that to limit replacements to only those inside a specific section or function in the editor window.

サインインしてコメントする。


Jan
Jan 2017 年 4 月 7 日
編集済み: Jan 2017 年 4 月 7 日
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.
  2 件のコメント
Petros
Petros 2022 年 12 月 1 日
the post sometimes does not show up
DGM
DGM 2022 年 12 月 1 日
The variable renaming functionality can stop working if you interrupt the editing of the variable name by moving away and moving back. There may be other conditions under which it doesn't work (e.g. if you're trying to rename across function scopes)

サインインしてコメントする。


dbmn
dbmn 2017 年 4 月 7 日
There is a simple solution for this, although it is ugly. But it takes minimal time to adapt.
Old Function
function [out] = myfun(a, b, c, d)
% yourcode...
end
New Function
function [out] = myfun(x)
a = x(1); b=x(2); c=x(3); d=x(4);
% yourcode...
end
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.

カテゴリ

Help Center および File ExchangeArgument Definitions についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by