フィルターのクリア

Rename variable throughout project/directory

16 ビュー (過去 30 日間)
Jason Klebes
Jason Klebes 2022 年 12 月 8 日
移動済み: Bruno Luong 2023 年 3 月 21 日
I have inherited a project (a director and a subdirectory of matlab files) where variable names are unhelpful. To make this clear to myself I want to rename some variables.
I know I can rename a variable throughtout a single file with shift enter or search. But it's tedious to do this for every file. Is there a way to rename/search and replace throughout a project?
  4 件のコメント
Stephen23
Stephen23 2022 年 12 月 8 日
編集済み: Stephen23 2022 年 12 月 8 日
"The variables names are often fields in structs"
Aaah, so structure fieldnames, not variables names. This important information is missing from your question.
"Maybe something wrong with this approach"
No, passing structure is a recommended way of passing lots of parameters between workspaces.
" Is there a way to rename/search and replace throughout a project?"
I recommend using Notepad++ features:
  • search and replace in multiple files under some folder
  • filters for filenames, extensions, etc.
  • search pattern using literals or regular expressions.
Stefan Schuberth
Stefan Schuberth 2023 年 3 月 21 日
移動済み: Bruno Luong 2023 年 3 月 21 日
Is this a joke? Every modern IDE can do that. This is called refactoring. Finally give more resources to Matlab's IDE department so they can write a worthy interface. Take a look at "Microsoft Code". Even Notepad++ can handle it! Just not Matlab.

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

採用された回答

Bruno Luong
Bruno Luong 2022 年 12 月 8 日
編集済み: Bruno Luong 2022 年 12 月 8 日
Use grep sed command of linux system. There light be some linux shell (cygwin?) on windows that you can install if you are under Windows.

その他の回答 (1 件)

Jan
Jan 2022 年 12 月 8 日
You can modify the field names using regexprep :
% Get a list of M-files:
List = dir(fullfile(Folder, '**', '.m'));
for k = 1:numel(List)
file = fullfile(List(k).folder, List(k).name);
str = fileread(file);
str = regexprep(str, 'Struct\.oldfield\s', '.newfield ');
writelines(str, file);
end
This does not consider sub-structs, or a directly following parenthesis or equal character. Hopefully a regexp hero can post a more powerful pattern.
But keep in mind, that the problem is not trivial. This modifies and comments strings also. After the conversion you should compare the new file with the original one and check all changes manually.

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by