Main Content

Find and Replace Text in Files

Find and Replace Any Text in Current File

You can search for, and optionally replace, any text within a file open in the Editor or Live Editor. To search for text in a file, on the Editor or Live Editor tab, in the Navigate section, click Find. You also can use the Ctrl+F keyboard shortcut.

In the find and replace dialog box, enter the text that you want to search for and then use the Find Next and Find Previous buttons to search forward or backward through the file. In the Live Editor and in MATLAB® Online™, use the down arrow and up arrow buttons or the F3 and Shift+F3 keyboard shortcuts instead.

To find text that matches the case of the search text, select the Match case check box. To find an exact full-word match, select the Whole word check box. In the Live Editor and in MATLAB Online, these options are available using the match case and whole word buttons.

To replace text in the file, enter the text that you want to replace the search text with and then use the Replace or Replace all buttons to replace the text. In the Live Editor and MATLAB Online, click the expand button to the left of the search field to open the replace options. Then, enter the text that you want to replace the search text with and use the replace and replace all buttons to replace the text.

Find and Replace Functions or Variables in 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.

Find references to a function or variable using automatic highlighting by following these steps:

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

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

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

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

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

To disable automatic highlighting, go to the Home tab and in the Environment section, click Preferences. In MATLAB > Colors > Programming Tools, clear the Automatically highlight option.

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.

Automatic variable and function renaming is enabled by default. To disable it:

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

  2. Select MATLAB > Editor/Debugger > Language.

  3. In the Language field, select MATLAB.

  4. Clear Enable automatic variable and function renaming.

In MATLAB Online, variable and function renaming preferences are located under MATLAB > Editor/Debugger > MATLAB Language.

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 or Live Editor tab, in the File section, click Find Files to open the Find Files dialog box. To open the Find Files dialog box in MATLAB Online, on the Editor or Live Editor tab, in the Navigate section, click Find and select Find Files. For more information, 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.

Go To Location in File

You can go to a specific location in a file, set bookmarks, navigate backward and forward within the file, and open a file or variable from within a file.

Navigate to a Specific Location

This table show how to navigate to a specific location within a file open in the Editor and Live Editor.

Go ToInstructionsNotes

Line Number

On the Editor or Live Editor tab, in the Navigate section, click Go To . Select Go to Line... and specify the line that you want to navigate to.

None

Function definition

On the Editor or Live Editor tab, in the Navigate section, click Go To . In the Function section, select the local function or nested function that you want to navigate to.

You also can select the file in the Current Folder browser and click the up arrow at the bottom of Current Folder browser to open the Details panel. Then, in the Details panel, double-click the function icon corresponding to the title of the function or local function that you want to navigate to.

Includes local functions and nested functions.

For both class and function files, the functions list in alphabetical order—except that in function files, the name of the main function always appears at the top of the list.

Code Section

On the Editor or Live Editor tab, in the Navigate section, click Go To . In the Sections section, select the title of the code section that you want to navigate to.

You also can select the file in the Current Folder browser and click the up arrow at the bottom of Current Folder browser to open the Details panel. Then, in the Details panel, double-click the section icon corresponding to the title of the section that you want to navigate to.

For more information, see Divide Your File into Code Sections.

Property

In the Current Folder browser, select the file that you want to navigate through and click the up arrow at the bottom of Current Folder browser to open the Details panel. Then, in the Details panel, double-click the property icon corresponding to the name of the property that you want to navigate to.

For more information, see Ways to Use Properties.

Method

In the Current Folder browser, select the file that you want to navigate through and click the up arrow at the bottom of Current Folder browser to open the Details panel. Then, in the Details panel, double-click the icon corresponding to the name of the method that you want to navigate to.

For more information, see Methods in Class Design.

Bookmark

On the Editor tab, in the Navigate section, click Go To . In the Bookmarks section, select Previous or Next.

To navigate to a bookmark in the Live Editor and in MATLAB Online, go to the Live Editor or Editor tab, and in the Navigate section, click Bookmark drop-down arrow. Then, select Previous or Next.

For information about setting and clearing bookmarks, see Set Bookmarks.

Note

The Details panel does not display details for live scripts or live functions and is not available in MATLAB Online.

Set Bookmarks

You can set a bookmark at any line in a file in the Editor or Live Editor so that you can quickly navigate to the bookmarked line. This is particularly useful in long files. For example, suppose that while working on a line, you want to look at another part of the file, and then return. Set a bookmark at the current line, go to the other part of the file, and then use the bookmark to return.

To set a bookmark in the Editor, position the cursor on the line that you want to add the bookmark to. Then, go to the Editor tab, and in the Navigate section, click Go To . Under Bookmarks, select Set/Clear. A bookmark icon appears to the left of the line. To clear the bookmark, with the cursor anywhere on the line with the bookmark, click Go To and under Bookmarks, select Set/Clear.

To set a bookmark in the Live Editor and in MATLAB Online, with the cursor on the line that you want to add the bookmark to, go to the Live Editor or Editor tab, and in the Navigate section, click Bookmark. To clear the bookmark, click Bookmark drop-down arrow, and select Set/Clear. You also can click the bookmark icon to the left of the line.

MATLAB does not maintain bookmarks after you close a file.

Navigate Backward and Forward in Files

In the Editor, you can access lines in a file in the same sequence that you previously navigated or edited them. To navigate backward and forward in sequence, on the Editor tab, in the Navigate section, click the left arrow and right arrow buttons. Backward and forward navigation is not supported in the Live Editor or in MATLAB Online.

Clicking the left arrow and right arrow buttons interrupts the backward and forward sequence. Editing a line or navigating to another line using the list of features described in Navigate to a Specific Location also interrupts the sequence.

Once the sequence is interrupted, you can still go to the lines preceding the interruption point in the sequence, but you cannot go to any lines after that point. Any lines that you edit or navigate to after interrupting the sequence are added to the sequence after the interruption point.

For example, in the Editor, open a file containing more than 6 lines and edit lines 2, 4, and 6. Click the left arrow button to return to line 4, and then again to return to line 2. Click the right arrow button to return to line 4. Edit line 3. This interrupts the sequence. You can no longer use the right arrow button to return to line 6. You can, however, click the left arrow button to return to line 2.

Open a File or Variable from Within a File

You can open a function, file, variable, or Simulink® model from within a file in the Editor or Live Editor. Position the cursor on the name, and then right-click and select Open selection from the context menu. Based on what the selection is, the Editor or Live Editor performs a different action, as described in this table.

Item

Action

Local function

Navigates to the local function within the current file, if that file is a MATLAB code file. If no function by that name exists in the current file, the Editor or Live Editor runs the open function on the selection, which opens the selection in the appropriate tool.

Text file

Opens in the Editor.

Figure file (.fig)

Opens in a figure window.

MATLAB variable that is in the current workspace

Opens in the Variables Editor.

Model

Opens in Simulink.

Other

If the selection is some other type, Open selection looks for a matching file in a private folder in the current folder and performs the appropriate action.