Matlab in Emacs
Why programming and debugging Matlab in Emacs? There are two main reasons:
- There are no Emacs emulators/keybindings available in Matlab on macOS, which makes Matlab programming less efficient.
- One might need to program and debug Matlab scripts remotely.
Introduction
In order to program Matlab in Emacs, it must be able to
- edit, including basic syntax, warning and error highlight, basic code completion, and
- debug, including running and debugging the scripts and basic command completion.
These features are supported by the MATLAB mode for Emacs, which provides the matlab-mode, matlab-shell and mlint-mode.
Setup
- Intall
matlab-modein Emacs,- either from Melpa using package manager, or
- clone the repo to a local directory and load it manually in the
init.el.
- Add the following snippet into the
init.el1
2
3
4
5
6;; associate .m file with the matlab-mode (major mode)
(add-to-list 'auto-mode-alist '("\\.m$" . matlab-mode))
;; setup matlab-shell
(setq matlab-shell-command "/Applications/MATLAB_R2021a.app/bin/matlab")
(setq matlab-shell-command-switches (list "-nodesktop"))
Usage
Editing
- Open a
.mfile,- It should be in
matlab-modewith syntax highlights - The
mlint-minor-modeshould be associated withmatlab-modeand be turned on by default, with warning/error highlights
- It should be in
C-M-ito complete symbol (call the functionmatlab-complete-symbol)C-M-aandC-M-eto jump to the beginning and end of a function ()- Jump to the line with warning/error highlights and
M-x mlint-show-warningto display the warning/error. M-x matlab-shell-describe-command(C-h RET f): open the help of a commandM-x matlab-shell-locate-fcn(C-c .): open the corresponding file of the function
Matlab-shell
M-x matlab-shellto open the matlab shell, use it the same as the Command Window in a Matlab application.- To run commands/scripts, one needs to go to the end of buffer
M->, to the>>, and type the command/script name to run it. - Press
<up>will run the commandmatlab-shell-previous-matching-input-from-input, and shows previous run commands. TABto complete commands- Variable related commands
workspaceto open the workspace browserwhoto list all variables in workspaceopen <varName>oropenvar <varNameto open a variable in the variable editor
Running and Debugging Scripts
Evaluate Sectionrun the commandsM-x matlab-shell-run-cell(C-M-return)Evaluate Selectionselect lines and run the commandsM-x matlab-shell-run-region(C-c C-r)matlab-emacsuse GUD (Grand Unified Debugger library) to support for debuggingC-x C-a C-b(gud-break()) to set breakpointsC-x C-a C-v(gud-list-breakpoints()) to list all breakpointsC-x C-a C-x(gud-remove()) to remove breakpoints
- When the program is running, the script (
.mfile) will entermatlab-shell-gud-minor-mode, and becomes read-only. Use the following commands for debuggingh, open helpbadd breakpoint, same asC-x C-a C-bxremove breakpoint, same asC-x C-a C-xvlist breakpoints, same asC-x C-a C-vsstep, same as runningdbstep inin the matlab shellnnext, same as runningdbstepin the matlab shellffinish function, same as runningdbstep outin the matlab shellccontinue, same as runningdbcontin the matlab shellqquit, same as runningdbquitin the matlab shell
gut-list-breakpointswill open a buffer undermlg-breakpoint-modelisting all breakpoints
Possible Problems
- The
mlintdoesn't show warnings/errors highlightsM-: matlab-toggle-show-mlint-warningsto toggle it on, or- add
(setq matlab-show-mlint-warnings t)to theinit.el
Error: no MLint program availablewhen trying to toggle on thematlab-toggle-show-mlint-warnings1
2
3;; setup mlint for warnings and errors highlighting
(add-to-list 'mlint-programs "/Applications/MATLAB_R2021a.app/bin/maci64/mlint") ;; add mlint program for macOS
(add-to-list 'mlint-programs "/usr/local/MATLAB/R2021a/bin/glnxa64/mlint") ;; add mlint program for Linux