Finding Text in Files

Finding Text in the Current File

Within the current file, select the text you want to find. From the Edit menu, select Find Selection. The next occurrence of that text is selected. 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. The previous occurrence of the text is selected. Repeat to continue finding the previous occurrences of the text.

Finding and Replacing Text in the Current File

You can search for specified text within multiple files, and then replace the text within a file.

Finding Text

To search for text in files, click the Find button in the Editor toolbar, or select Edit > Find and Replace. Complete the resulting Find Replace dialog box.

Image of the Find & Replace dialog box.

The search begins at the current cursor position. The Editor finds the text you specified and highlights it. To find another occurrence, click Find Next or Find Previous, or use the keyboard shortcuts F3 and Shift+F3 (or Command+F3 and Command+Shift+F3 with Macintosh® key bindings).

The MATLAB® software beeps when a search for Find Next reaches the end of the file, or when a search for Find Previous reaches the top of the file. If you have Wrap around selected, it continues searching after beeping.

Use F3 and Shift+F3 to continue finding the specified text even after closing the Find Replace dialog box. You can go to another file and find the specified text in it.

Change the selection in the Look in field to search for the specified text in other Microsoft® desktop tools.

Replacing Text

After finding text using the Find Replace dialog box, you can replace the text in the current file:

  1. In the Replace with field, type the text that is to replace the found text.

  2. Click Replace to replace the text currently selected, or click Replace All to replace all instances in the current file.

    The text is replaced. For Replace All, the number of instances that were replaced appears in the Editor status bar.

  3. To save the changes to the file, select Save from the File menu.

You can repeat this for multiple files.

Function Alternative for Finding Text

Use lookfor to search for the specified text in the first line of help for all M-files on the search path.

Finding Files or Text in Multiple Files

To find directories and file names that include specified text, or whose contents contain specified text, use Edit > Find Files. For details, see Finding Files and Content Within Files.

Incremental Search

With the incremental search feature, 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. Incremental search is also available in the Command Window—see Incremental Search.

To use the incremental search feature in the Editor, follow these steps:

  1. Position the cursor where you want the search to begin.

  2. How you begin the incremental search depends on your setting for the Editor/Debugger key bindings preference and in which direction you want to search:

    An incremental search field appears in the left side of the status bar of the current file window. F Inc Search means search Forward from the cursor. The field label is instead R Inc Search when you search backwards.

    Image of Editor with file in it, showing the incremental search field in the lower left corner. The F Inc Search means the increment search will search forward from the cursor.

  3. In the incremental search field, type the text you want to find. For example, type plot.

    As you type the first letter, p, the first occurrence of that letter after the cursor is highlighted. In the example shown, the cursor is in the middle of line 2, so the first occurrence of p, the p in problem on line 2, is highlighted.

    Image of first three lines in a file in the Editor showing results of incremental search. Line 2 is % Plot length of sequence for Collatz problem. The cursor is positioned before the word

    Incremental search is case sensitive for uppercase letters. In the above example, searching for uppercase P, would instead find the P in Prepare on line 3.

    When you type the next letter in the term you are searching for, the first occurrence of the term becomes highlighted. In the example, when you add the letter l to the p so that the incremental search field now has pl, the pl in plot on line 8 is highlighted. When you add ot to the term in the incremental search field, the whole word plot in line 8 is highlighted.

  4. To find the next occurrence of plot in the file, press Ctrl+S. To find the previous occurrence of the text, press Ctrl+R.

  5. If MATLAB beeps, it either means the search is at the end or beginning of the file, or it means that the text was not found.

  6. To end the incremental search, press Esc or Enter, or any other noncharacter or number key except Tab or backspace.

    The incremental search field no longer appears in the status bar. The cursor is now located at the position where the string was last found.

If you press Ctrl+S or Ctrl+R after displaying the blank incremental search field, the search term from your previous incremental search appears in the field. Then the backspace key deletes the entire previous search term, rather than just the last letter.

  


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