Documentation Center

  • Trial Software
  • Product Updates

Find and Replace Text in Files

Find Any Text in the Current File

  1. Within the current file, select the text you want to find.

  2. On the Editor tab, in the Navigate section, click Find , and then select Find....

    A Find & Replace dialog box opens.

  3. Click Find Next to continue finding more occurrences of the text.

To find the previous occurrence of selected text (find backwards) in the current file, click Find Previous on the Find & Replace dialog box.

Find and Replace Functions or Variables in the Current File

To search for references to a particular function or variable, use the automatic highlighting feature for variables and functions. This feature is more efficient than using the text finding tools. Function and variable highlighting indicates only references to a particular function or variable, not other occurrences. For instance, it does not find instances of the function or variable name in comments. Furthermore, variable highlighting only includes references to the same variable. That is, if two variables use the same name, but are in different scopes, highlighting one does not cause the other to highlight.

  1. On the Home tab, in the Environment section, click Preferences.

    The Preferences dialog box opens.

  2. Select MATLAB > Colors > Programming Tools.

  3. Under Variable and function colors, select Automatically highlight, deselect Variables with shared scope, and then click Apply.

  4. In a file open in the Editor, click an instance of the variable you want to find throughout the file.

    MATLAB® indicates all occurrences of that variable within the file by:

    • Highlighting them in teal blue (by default) throughout the file

    • Adding a marker for each in the indicator bar

      If a code analyzer indicator and a variable indicator appear on the same line in a file, the marker for the variable takes precedence.

  5. Hover over a marker in the indicator bar to see the line it represents.

  6. Click a marker in the indicator bar to navigate to that occurrence of the variable.

    Replace an instance of a function or variable by editing the occurrence at a line to which you have navigated.

The following image shows an example of how the Editor looks with variable highlighting enabled. In this image, the variable i appears highlighted in sky blue, and the indicator bar contains three variable markers.

Automatically Rename All Functions or Variables in a File

To help prevent typographical errors, MATLAB provides a feature that helps rename multiple references to a function or variable within a file when you manually change any of the following:

Function or Variable RenamedExample

Function name in a function declaration

Rename foo in:

function foo(m)

Input or output variable name in a function declaration

Rename y or m in:

function y = foo(m)

Variable name on the left side of assignment statement

Rename y in:

y = 1

As you rename such a function or variable, a tooltip opens if there is more than one reference to that variable or function in the file. The tooltip indicates that MATLAB will rename all instances of the function or variable in the file when you press Shift + Enter.

Typically, multiple references to a function appear when you use nested functions or local functions.

    Note:   MATLAB does not prompt you when you change:

    • The name of a global variable.

    • The function input and output arguments, varargin and varargout.

To undo automatic name changes, click once.

By default, this feature is enabled. To disable it:

  1. On the Home tab, in the Environment section, click Preferences.

    The Preferences dialog box opens.

  2. Select MATLAB > Editor/Debugger > Language.

  3. In the Language field, select MATLAB.

  4. Clear Enable automatic variable and function renaming.

Find and Replace Any Text

You can search for, and optionally replace specified text within a file. On the Editor tab, in the Navigate section, click Find to open and use the Find & Replace dialog box.

Find Text in Multiple File Names or Files

You can find folders and file names that include specified text, or whose contents contain specified text. On the Editor tab, in the File section, click Find Files to open the Find Files dialog box. For details, see Find Files and Folders.

Function Alternative for Finding Text

Use lookfor to search for the specified text in the first line of help for all files with the .m extension on the search path.

Perform an Incremental Search in the Editor

When you perform an incremental search, the cursor moves to the next or previous occurrence of the specified text in the current file. It is similar to the Emacs search feature. In the Editor, incremental search uses the same controls as incremental search in the Command Window. For details, see Incremental Search Using Keyboard Shortcuts.

Was this topic helpful?