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-mode
in 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.el
1
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
.m
file,- It should be in
matlab-mode
with syntax highlights - The
mlint-minor-mode
should be associated withmatlab-mode
and be turned on by default, with warning/error highlights
- It should be in
C-M-i
to complete symbol (call the functionmatlab-complete-symbol
)C-M-a
andC-M-e
to jump to the beginning and end of a function ()- Jump to the line with warning/error highlights and
M-x mlint-show-warning
to 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-shell
to 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. TAB
to complete commands- Variable related commands
workspace
to open the workspace browserwho
to list all variables in workspaceopen <varName>
oropenvar <varName
to open a variable in the variable editor
Running and Debugging Scripts
Evaluate Section
run the commandsM-x matlab-shell-run-cell
(C-M-return
)Evaluate Selection
select lines and run the commandsM-x matlab-shell-run-region
(C-c C-r
)matlab-emacs
use 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 (
.m
file) will entermatlab-shell-gud-minor-mode
, and becomes read-only. Use the following commands for debuggingh
, open helpb
add breakpoint, same asC-x C-a C-b
x
remove breakpoint, same asC-x C-a C-x
v
list breakpoints, same asC-x C-a C-v
s
step, same as runningdbstep in
in the matlab shelln
next, same as runningdbstep
in the matlab shellf
finish function, same as runningdbstep out
in the matlab shellc
continue, same as runningdbcont
in the matlab shellq
quit, same as runningdbquit
in the matlab shell
gut-list-breakpoints
will open a buffer undermlg-breakpoint-mode
listing all breakpoints
Possible Problems
- The
mlint
doesn't show warnings/errors highlightsM-: matlab-toggle-show-mlint-warnings
to toggle it on, or- add
(setq matlab-show-mlint-warnings t)
to theinit.el
Error: no MLint program available
when trying to toggle on thematlab-toggle-show-mlint-warnings
1
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