Automatically close parenthesis when we open it

153 views (last 30 days)
As you know in other interpreters and compilers we have a good functionality that when we open a parenthesis (or {} and []), editor automatically close it. Is anyway to add this to MATLAB R2014b?
  1 Comment
Yamm on 16 Jan 2023
Edited: Yamm on 25 Jan 2023
I think it could be solved with ahk scripts.

Sign in to comment.

Answers (4)

John D'Errico
John D'Errico on 14 Feb 2015
Edited: John D'Errico on 15 Feb 2015
Yes. To some extent, but currently only at the command line.
>> (2+3
Error: Expression or statement is incorrect--possibly unbalanced (, {, or [.
Did you mean:
>> (2+3)
But remember that the computer may not be able to correctly infer your intentions when you write code. For example, what was intended here:
>> reshape([2+3,5,1)
Error: Unbalanced or unexpected parenthesis or bracket.
Did you mean:
>> reshape([2+3,5,1])
Did MATLAB guess wrong, as perhaps my intent was to write this instead:
At the current time, the only automatic completion offered, i.e., closing of parens, is at the command line, in the simple fashion shown above. I would point out that this is often a precursor to the editor being able to do something similar in a future release, as they seem to test out ideas like this slowly.
A point that I should make is that there is often some ambiguity in such a completion, and the dangers inherent in any such action. For example, there are now spellcheckers that run automatically as you type. Great to some extent, but rarely perfect. Since I type somewhat quickly, and I know the keyboard well enough, I often do not need to pay much attention to either the keyboard or the screen. My mind is instead focused on the next sentence, or the next line of code, planning out what I intend to say. Normally, I'd look back after a line or so at what I typed, and any errors will be glaring.
So what happens instead is those automatic spellcheckers tend to insert their corrections for me. They may in fact replace a word that does not exist in their dictionary with the wrong word, and do so while I type. Since the error is not a glaring mistake, it becomes more difficult for me to correct.
I turn those spellcheckers off whenever I can do so, as my own grasp of spelling is better than theirs, especially when you include areas like mathematics and MATLAB code.
As I pointed out above, since the correction may be somewhat ambiguous as to where the correct placement should be, I don't want the editor doing the correction for me, as I know how to code better than an editor does.
You can of course change the editor, using your own choice of tools for that purpose. If that editor has a MATLAB mode, as some do, then it might also allow such completion. If you do make a change, you then do lose some capability that is useful. For example, the mlint flags in the current editor can be helpful.
Walter Roberson
Walter Roberson on 16 Jan 2023
(There were a small number of improvements made to the traditional editor. But then eventually the traditional editor was removed.)

Sign in to comment.

emehmetcik on 14 Feb 2015
I think this is something related to the text editor preferences. You can try changing the editor used by Matlab. In File->Preferences->Editor/Debugger check "Text editor" and select the editor you want to use (e.g. notepad++).
John D'Errico
John D'Errico on 15 Feb 2015
Edited: John D'Errico on 15 Feb 2015
Yes. I wondered if that was the goal. However, the current editor allows nothing of the sort at the moment. The only case where any automatic completion is offered is currently at the command line. I point out some issues in my answer.

Sign in to comment.

Jan on 13 May 2018
The auto-completion works sufficiently in NotePad++: Enable it in Settings -> Preferences -> Auto Completion -> Select the wanted checkboxes.
I used FEX: EditorMacro for the auto-completion in Matlab's editor. But after I got used to it, a new Matlab version broke the compatibility. In addition I'm working on many different computers and getting accustomed to a non-standard typing is more confusing than closing the parentheses manually.

Yamm on 25 Jan 2023
Edited: Walter Roberson on 25 Jan 2023
Hi, I was able to find a nice solution to this using and AHK script, probaby can be written better:
Send, (){Left}
Send, []{Left}
Send, {{}{}}{Left}


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


No tags entered yet.


Community Treasure Hunt

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

Start Hunting!