Main Content

编辑和格式代码

要编辑您的代码,在编辑器和实时编辑器中,您可以使用列选择,代码自动完成和重构。要格式化代码并使您的代码易于阅读,使用凹痕,文本宽度指示和代码折叠。

Column Selection

When adding or editing code in the Editor and Live Editor, you can select and edit a rectangular area of code (also known ascolumn selectionor块编辑)。如果要复制或删除多个数据列(与行相对),或者您一次一次编辑多行,则选择和编辑代码很有用。要选择矩形区域,请按一种lt钥匙在使用鼠标进行选择时。在macOSsystems, use theOption钥匙。

For example, select the second column of data in一种

Type0将所有选定的值设置为0。

Before R2021b, column selection is available only in the Live Editor, not in the Editor.

更改案例

You can change the case of selected text or code from all uppercase to lowercase, or vice versa, in the Editor and Live Editor. Select the text, right-click, and select更改案例。一种lternatively, you can pressCtrl+Shift+A。如果文本包含大写和小写文本,则MATLAB®changes the case to all uppercase.

在R2021b之前更改案例option is available only in the Live Editor, not in the Editor.

一种utomatically Complete Code

MATLAB completes parentheses and quotes when entering code in the Editor and Live Editor. For example, if you type an open parenthesis in the Editor or Live Editor, MATLAB adds the closing parenthesis. MATLAB also completes comments, character vectors, strings, and parentheses when split across two lines.

MATLAB也可以完成块结尾。在首页tab, in theEnvironment部分,单击首选项。选择Editor/Debugger>一种utomatic Completions一种utocoding options部分,选择一个或多个一种utocomplete block endings选项。

取消一个automatic code completion, pressCtrl+Zor theUndobutton. To disable automatic code completions, in theEditor/Debugger>一种utomatic Completionspreferences, clear one or more of the options in the一种utocoding optionssection.

在R2021B之前,MATLAB仅在实时编辑器中完成代码,而不是在编辑器中完成代码。

重构代码

您可以通过将代码的选定区域转换为函数或本地功能(称为代码),将大型脚本或功能分解为较小的部分重构

To refactor a selected area of code:

  1. 选择one or more lines of code.

  2. Editoror现场编辑tab, in the代码部分,单击Refactorand select from the available options.

  3. 输入新功能的名称。MATLAB使用选定的代码创建功能,并用调用新创建的函数替换原始代码。

在R2021B之前,仅在实时编辑器而不是编辑器中可用重构选项。

Indent Code

缩进的代码制作功能和语句,例如while循环易于阅读。默认情况下,MATLAB INDENTS代码(例如函数和编辑器和Live Editor)在您输入时。当您使用标签或空格缩进线时,MATLAB还将后续行与这些行对齐。

您可以根据您喜欢编写代码的方式启用或禁用自动缩进。在首页tab, in theEnvironment部分,单击首选项。选择MATLAB>Editor/Debugger>语言语言下拉列表,选择a programming language. Then, in theIndenting选定语言的部分,选择或清除一种pply smart indenting while typingoption.

笔记

Indenting preferences are not supported for TLC, VHDL, or Verilog.

InMatlab Online™, indenting preferences are located underMATLAB>Editor/Debugger>MATLAB LanguageandMATLAB>Editor/Debugger>其他语言

To indent selected lines of code if automatic indenting is disabled, go to theEditoror现场编辑标签和代码部分,单击

手动将选定线的缩进进一步向左或向右增加Editoror现场编辑选项卡,单击, 或者。无论启用或禁用自动缩进,手动增加缩进的工作原理。或者,您可以使用Tab钥匙或Shift+Tab钥匙分别。如果您选择emacs风格的选项卡键智能缩进option in theMATLAB>Editor/Debugger>Tabpreferences, the selected lines indent according to indenting practices.

在R2018A之前,仅在编辑器中支持缩进的首选项,而不是在实时编辑中。金宝app

指定功能缩进首选项

You can specify how functions indent in MATLAB code files. On the首页tab, in theEnvironment部分,单击首选项。选择MATLAB>Editor/Debugger>语言语言下拉列表,选择MATLAB。然后,从功能缩进格式选项:

  • 经典的— The Editor and Live Editor align the function code with the function declaration.

    包含代码和嵌套函数的函数,每个函数的代码都与函数声明对齐。

  • 缩进的嵌套功能— The Editor and Live Editor indent the function code within a nested function.

    包含代码和嵌套函数的函数,并在嵌套函数声明中缩进嵌套函数中的代码。

  • Indent all functions- 编辑和实时编辑器缩进主功能和嵌套功能的功能代码。

    Function containing code and a nested function, with the code for each function indented from their function declaration.

InMATLAB在线,MATLAB缩进的首选项位于MATLAB>Editor/Debugger>MATLAB Language

Fold Code

代码折叠扩展和折叠编辑器中MATLAB代码的块。您可以使用代码折叠来隐藏您当前未处理的代码。代码折叠可改善包含许多功能或其他代码块的文件的可读性。实时编辑器中不支持代码折叠。金宝app

For example, you can fold:

  • 代码sections

  • 为了andparfor

  • Function code

  • Class code

  • Multiline comments

要扩展或折叠代码块,请单击加号or minus signthat appears to the left of the construct in the Editor. Alternatively, you can use theCtrl+Shift+。(时期)andCtrl+. (period)键盘快捷键或使用代码折叠按钮Viewtab.

要扩展或折叠文件中的所有代码,请将光标放在文件中的任何位置,请转到View选项卡,然后选择Expand AllorCollapse All。或者,您可以使用Ctrl+Shift+, (comma)andCtrl+,(逗号)键盘快捷键。

笔记

如果您打印一个或多个折叠构造的文件,则这些构造将在文件的打印版本中展开。

You can change which programming constructs can be folded and whether a programming construct is collapsed the first time that you open a MATLAB file. On the首页tab, in theEnvironment部分,单击首选项。选择Editor/Debugger>代码Folding, and then adjust the preference options.

Change the Right-Side Text Limit Indicator

By default, a light gray vertical line (rule) appears at column 75 in the Editor, indicating where a line exceeds 75 characters. You can set this text limit indicator to another value, which is useful, for example, if you want to view the code in another text editor that has a different line width limit. The right-side text limit indicator is not supported in the Live Editor.

隐藏或更改垂直线的外观:

  1. 首页tab, in theEnvironment部分,单击首选项

  2. 在首选项窗口中,选择MATLAB>Editor/Debugger>展示

  3. 一种djust the settings in theRight-hand text limitsection.

右侧文本限制指示器仅是视觉提示,并且不能阻止文本超过限制。要自动以指定的列编号包装评论文本,请转到首页标签和Environment部分,单击首选项。选择MATLAB>Editor/Debugger>语言, and adjust theComment formattingpreferences. To adjustComment formattingpreferences inMATLAB在线, 选择Editor/Debugger>MATLAB Language

Related Topics