Matlab Editor: How to limit a "replace" to just the selected text?

83 views (last 30 days)
Matlab Editor: How to limit a "replace" to just the selected text?
I don't see a check-box for this.
It would be very useful when searching and replacing.
Is this even possible in the Matlab editor (cf the Visual Studio editor).
Walter Roberson
Walter Roberson on 23 Dec 2020
I seem to recall reading that this is supported in the Live Script editor, but it would take me research to locate that. Live Script editor gets new features first.

Sign in to comment.

Accepted Answer

Sam Johnson
Sam Johnson on 21 Sep 2012
I have submitted the replaceInSelection function to the File Exchange which solves this problem using the MATLAB Editor API. Having selected text in the editor window you can call replaceInSelection(oldSubStr, newSubStr) to perform a limited replace. I hope this helps.

More Answers (7)

AwedBy Matlab
AwedBy Matlab on 19 Oct 2012
It's beyond belief that MathWorks are ignoring user requests to implement this very basic feature! Right now if you want to do a find&replace within a selection of your code, you have to Cut the text to a word processor, do the find&replace there, then Paste it back into Matlab. Completely ludicrous that they release two versions a year, yet it doesn't cross their minds to add this essential feature!
  1 Comment
Walter Roberson
Walter Roberson on 19 Oct 2012
We don't know that it hasn't crossed their minds: we only know that it isn't publicly available through any known commands.

Sign in to comment.

Jan on 14 May 2011
No, this is not possible in the Matlab editor. Let's ask Yair, if his EditorMacro could solve this.
Walter Roberson
Walter Roberson on 4 Aug 2021
Ed Callway comments to Babak Zandi:
Yup, I waste HOURS doing search and replace over large areas by hand. Every other editor trivially handles search/replace on highlighted text. C'mon, this is a 1 week coop project, not a giant rewrite.

Sign in to comment.

the cyclist
the cyclist on 14 May 2011
I am not 100% sure if this is what you mean, but if you select some text, and "copy" it (e.g. with cntl-c in Windows) before opening the search/replace window with cntl-f, then that text will appear in the "Find What" textbox, and you can replace it.
  1 Comment
Kevin Claytor
Kevin Claytor on 20 Sep 2012
I don't think that's what he means, but FYI - you don't even have to copy the text first, just highlighting the text and pressing ctrl-f should auto-fill the "find" box.

Sign in to comment.

Micky Harris
Micky Harris on 7 Jan 2015
I think the question is how to do multiple replacements in the selected text of the open file. Or another way to say this limit the scope of replacement to only what is selected. The other editors i use notepad++ and nedit have this feature.
Walter Roberson
Walter Roberson on 6 Sep 2016
James Bell, C as a programming language was not started until 1971, so if Mathworks has a bunch of 60's C programmers then they would have to be time travelers.

Sign in to comment.

Leonard König
Leonard König on 24 Aug 2017
Sam Johnson's Solution works! Not as comfortable as a checkbox in the search and replace GUI, but at least it works. Sadly that MathWorks wasn't able to add this funktion in the past 6 years.
Sinan Islam
Sinan Islam on 9 Feb 2021
I also get surprised when MATLAB lack the most basic tools and options.

Sign in to comment.

WAPAstobbem on 21 Jun 2018
For me, the replace feature is about 50% working. I can highlight a section of text, but a Replace All will still replace from that cursor point to the end of the file. However, everything before the cursor point remains fine. So, I'm doing something wrong with the controls, or else the end cursor point is not yet considered under Replace All. Individual Replace seems to be working just fine.

Aron Magesh
Aron Magesh on 22 Sep 2021
Yes, it strange that the Find and Replace feature does not work for selected part of the code. I have to copy the selected text/code to a blank script, do F&R and then paste back.


Find more on Environment and Settings in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!