Skip to Main Content Skip to Search
Product Documentation

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. From the Edit menu, select Find Selection.

    The next occurrence of that text is selected.

  3. Select Find Selection again (or Find Next) to continue finding more occurrences of the text.

To find the previous occurrence of selected text (find backwards) in the current file, select Find Previous from the Edit menu.

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. Select File > Preferences > Colors > Programming Tools.

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

  3. 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.

    • Specifying the number of usages it finds in the status bar

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

  5. 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, the indicator bar contains three variable markers, and the status bar (on the lower left edge of the tool) indicates 5 usages of "i" found.

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 subfunctions.

To undo automatic name changes, click the Undo button once.

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

  1. Select File > Preferences > Editor/Debugger > Language.

  2. In the Language field, select MATLAB.

  3. Clear Enable automatic variable and function renaming.

Find and Replace Any Text

To search for, and optionally replace specified text within a file, select Edit > Find and Replace to open and use the Find & Replace dialog box.

Find Text in Multiple File Names or Files

To find folders and file names that include specified text, or whose contents contain specified text, select Edit > Find Files. For details, see Finding 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 Use Incremental Search in the Command Window.

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS