Clean and automate indents in scripts an live scripts

6 ビュー (過去 30 日間)
Silvio Rizzuto
Silvio Rizzuto 2025 年 6 月 19 日
編集済み: Silvio Rizzuto 2025 年 6 月 23 日
Hi everyone,
Please, do you have any tips to automate indentation in scripts?
Example by the picture below:
  1 件のコメント
Catalytic
Catalytic 2025 年 6 月 19 日
Please do not insert screenshots of code when text will suffice. It prevents us from copy/pasting the example.

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

採用された回答

Matt J
Matt J 2025 年 6 月 19 日
編集済み: Matt J 2025 年 6 月 19 日
Download my editorExtract and editorReplace tools,
Then add the function below as a Quick Access Toolbar button. You can then highlight the text you want to modify and apply the button to obtain text with all the "=" signs lined up.
function favoriteAlignEqualSigns()
[txt,info]=editorExtract(1);
before= char(extractBefore(txt,"="));
after = char(extractAfter(txt,"="));
newtxt=before+" = "+after;
editorReplace(newtxt,info);
end
Note that this function assumes one assignment statement per line of code (like in your example), and does no error checking to that effect.
  1 件のコメント
Silvio Rizzuto
Silvio Rizzuto 2025 年 6 月 19 日
Thank you so much Matt!
Your toolbox seems a good first step. I will test it as soon as possible!

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

その他の回答 (1 件)

Les Beckham
Les Beckham 2025 年 6 月 19 日
I think that Matt's answer is a bit more elegant, although I think it (at least as first posted) just adds spaces before and after each equals sign rather than lining them up. I don't see anything that detects which column they should be aligned on. Here is a function which does do that.
% alignEquals.m
%
% Lines up the equals signs in the selected text in the active Matlab editor document.
function alignEquals()
ed = matlab.desktop.editor.getActive;
selected_lines = matlab.desktop.editor.textToLines(ed.SelectedText); % cell array containing the selected lines
eqIdx = strfind(selected_lines, '='); % find the index for all of the equals signs
maxeqIdx = max([eqIdx{:}]); % index of the farthest right equals sign
% align all of the equals signs with the farthest right equals sign
for iLine = 1:numel(selected_lines)
if isempty(selected_lines{iLine}), continue, end % skip empty lines
if isempty(eqIdx{iLine}), continue, end % skip lines with no equals sign
selected_lines{iLine} = strrep(selected_lines{iLine}, '=', [repelem(' ', maxeqIdx - eqIdx{iLine}) '=']);
end
newText = matlab.desktop.editor.linesToText(selected_lines); % selected text with the equals signs lined up
ed.Text = strrep(ed.Text, ed.SelectedText, newText); % replace the text in the editor document
end
  4 件のコメント
Silvio Rizzuto
Silvio Rizzuto 2025 年 6 月 23 日
Thank you very much for your interest and support.
Glad to get from community her help. 👏💪
Silvio Rizzuto
Silvio Rizzuto 2025 年 6 月 23 日
編集済み: Silvio Rizzuto 2025 年 6 月 23 日
Please, let me add an additionnal detail after using. Avoid "=" in comments. I changeet for ":" and everythings go good.

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

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by