Customized text modification buttons for the Matlab Editor

Version 1.2.0 (162 KB) by Matt J
A pair of functions enabling simple, programmatic modification of Matlab Editor text. These can be used to make customized buttons.
25 Downloads
Updated 12 Jun 2025

View License

Overview
This submission defines a pair of commands that make it easy to modify Matlab Editor text programmatically. The function editorExtract() will extract the region of text currently highlighted by the user in the active Matlab Editor tab. This text can be modified, and then reinserted using editorReplace(). When used in combination, these tools provide a convenient way to code Quick Access buttons in the Editor, thereby expanding the available range of text-modifying Editor functions.
Example 1:
The following converts a selected region of Editor code to uppercase,
[txt,info]=editorExtract();
editorReplace(upper(txt),info);
The effect of this is illustrated in the figures below. After running the above (do this with a Quick Access button or from the command line), the region selected in the Editor (left figure) will become capitalized (right figure).
Example 2:
The following adds indentation to code comments. The indentation is inserted AFTER the '%'.
[txt,info]=editorExtract(true);
editorReplace(replace(txt,'%','% '),info)
The effect of this is illustrated in the figures below.
Notice that it was not necessary, in this example, to highlight the entire affected region of code (the first and last row are only partially selected). This behavior was achieved by calling editorExtract(true) with an optional boolean input flag, which tells editorExtract() that complete rows are to be extracted, even if only partially highlighted.
Prepackaged Button Code:
As mentioned, these tools are particularly useful for adding 'Favorite Command' buttons to the Quick Access bar, thereby expanding the variety of text manipulation functions available in the Editor. To showcase this, I have included some of my own Favorite button definitions in this package, built using editorExtract() and editorReplace(). These are described in the following subsections, together with figure illustrations. I have also included the image files (.png) that I use to provide customized icons for some of the buttons.
favoriteStripSemicolons.m: This button will strip the semicolons from the lines of code in the selected region, so that their output will no longer be suppressed. This can be useful during debugging.
favoriteForceSemicolons.m: This button will add semicolons to unsuppressed lines of code. It is possible to exempt specific lines using the pragma %<NOSMC>.
favoriteLegacyCommenting.m: In older versions of Matlab, Ctrl+R would comment out the highlighted lines in the Editor, except that the '%''s would always be inserted at the beginning of the line. Conversely, in more recent Matlab, they are inserted following any indenting whitespace present. This button inserts '%''s in the oiriginal, legacy manner.
favoriteTicToc.m: This will enclose the highlighted area in a tic...toc sequence. It will also remove an enclosing tic/toc pair, if already present.
favoriteSwitchCase.m: This button will insert an empty switch/case ladder into the code for the user to populate. The user is GUI-prompted to enter the number of cases in the ladder.
favoriteUnstruct.m: Sometimes, I will use an arguments block to validate name-value parameter pairs. A side effect of this, however, is that the values are wrapped in an options struct, which is not always desirable. This button will generate a series of statements that unpack the struct fields into separate variables, so that they may be more succinctly referenced in the remainder of the code.
favoriteFileRename.m: If you have an mfunction file whose signature line doesn't match the file name, the Code Analyzer alerts you and offers to modify the signature to match the file name. Sometimes, however, you might want the converse, namely to rename the file to match the signature. This button will do that. It works similarly for classdefs.
favoriteFuncHeader.m: Apply this button function while highlighting any portion of a functions signature line (or just put the cursor there). It will add template help text and an arguments block below the signature. This works for any function (local, nested, class method, etc...).
favoriteArgs2Help.m: This button is another help text generator of a somewhat different flavor than favoriteFuncHeader. Here, the idea is to start with a function with perhaps minimal help text, but with arguments...end blocks containing detailed descriptions of each argument in %-comments . When you mouse-select the arguments blocks and apply the button, the arguments block contents will be reformatted into help text, which is then inserted above the arguments blocks, overwriting any help text that was included in the selected region. Note that the (Output) arguments block is optional. This is again applicable to any function (local, nested, class method, etc...).

Cite As

Matt J (2025). Customized text modification buttons for the Matlab Editor (https://www.mathworks.com/matlabcentral/fileexchange/181000-customized-text-modification-buttons-for-the-matlab-editor), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2024b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes
1.2.0

*Fixed a bug occurring when editor text contains explicit '\n' characters.

1.1.10

Small bug in editorReplace.m

1.1.9

Forget to upload the .zip for 1.1.8

1.1.8

*Added favoriteTicToc
*Fixed double commenting in favoriteFuncHeader
*favoriteFuncHeader now provides an Output arguments block

1.1.7

favoriteFuncHeader now also generates an arguments (Output) ... end block

1.1.6

*Changed conventions of favoriteArgs2Help: Outputs will now be post-pended

1.1.5

Description edit

1.1.4

Description edit

1.1.3

Added favoritesArgs2Help.m

1.1.2

*Improved robustness of favoriteUnstruct
*With favoriteStripSemicolons, using the pragma %<KEEPSMC> will prevent that semicolons on that line from being stripped.

1.1.1

Description update

1.1.0

*Improved parsing robustness of favoriteStripSemicolons and favoriteForceSemicolons
*Improved indentation preservation in favoriteSwitchCase
*Improved the workflow and of favoriteUnstruct (see help doc)
*Added a new button function favoriteFuncHeader

1.0.8

small bug fix in extractEditor

1.0.7

More uniform indentation of inserted text in favoriteSwitchCase

1.0.6

Figure improvement

1.0.5

*Cosmetic fixes to editorReplace
*Added favoriteFileRename

1.0.4

Bug fix in favoritesStripSemicolons.m
Now handles lines with trailing comments.

1.0.3

Description edit

1.0.2

*Description modifications
*Added favoritesUnstruct.m button code

1.0.1

Description edits

1.0.0