I am new to gnu Emacs and have a question. I created an M file to run in Matlab and I got it working fine. My question is, is there a way to compile my code in the Emacs editor or test out my program? I tried the to use the "make" command and it gave me this error: Compilation exited abnormally with code. Thank you in advance for your help.

  • By compilation, do you mean simply running the matlab script? matlab-mode comes with matlab shell. You simply launch the shell and run the script as you would do in the consol in Matlab GUI. Commented Jul 18, 2015 at 2:09
  • Yes, I want to run the matlab script. Where can I find matlab-mode or the matlab shell? Sorry I am brand new at this!
    – Jack
    Commented Jul 18, 2015 at 2:12
  • When I was working in Matlab, the matlab-mode was not maintained any more and I simply copied the one my school provided to my init and it has worked fine till date (the latest Matlab version I tried on it was 2014). This version by pronobis has a lot more updates to the version I use but I haven't tried it yet. I can write a detailed answer at a later time. Commented Jul 18, 2015 at 2:26
  • Thank you! It seems sourgeforce has one but the site is down. I will try your links. Thank you again!
    – Jack
    Commented Jul 18, 2015 at 2:48

4 Answers 4


I don't use MATLAB, but this sounded like a problem I had when first starting to use emacs.

Did you originally try M-x compile to compile your code? That will run the shell command specified by the next input. The default is make, which is used for c programming. Here's the documentation: http://www.gnu.org/software/emacs/manual/html_node/emacs/Compilation.html#Compilation

Instead, you want to use the command line program that compiles your MATLAB code. (Is it mcc?). So the input after M-x compile should be something like

mcc [-options] mfile1

What ever the command, it needs to be in your path variable.


Start by installing el-get; then, juts el-get-install and select matlab-mode. Then, have a look to INSTALL and README. Here you'll find all you need. Don't forget to

(require 'matlab-load) 

At this point, you'll be able to launch the matlab shell as an inferior process, evaluate regions, lines, etc., and use mlint to check your code.

Note that you may achieve the same using the provided matlab-mode package when using melpa instead of el-get. No instruction files in this case, though.

  • Is this the same as the matlab-mode that's available in melpa?
    – user2699
    Commented Dec 11, 2015 at 1:33
  • Reading el-get's and melpa's recipes, they come from the same source. Is there an advantage to using el-get instead of package-install? If not, maybe this answer should suggest that instead, it seems to be the more "official" alternative
    – timor
    Commented Dec 11, 2015 at 15:14
  • They are pretty similar, except that melpa version is more compact, no project files, no makefiles ... and not INSTALL or README files. This is why I privilege el-get.
    – csantosb
    Commented Dec 12, 2015 at 14:40

You can run bash in a new buffer in Emacs by pressing C-x 2 (or C-x 3). Then go to that buffer and press M-x shell to run bash.

In order to run matlab in console mode you need to do the following in bash:

matlab -nodisplay -nosplash -nodesktop -r "run('yourfile.m');exit"

It's better to write the above command in a shell script that accepts the matlab file as an input.


If matlab-mode isn't installed, by default .m files are loaded in octave mode. If you aren't familiar with octave, it can run most matlab code. (Functions from some of the toolboxes aren't implemented, and some newer features are absent. For most basic things though, matlab code will run as-is in octave). This is a good option if matlab isn't available.

To get a console, use the command run-octave, which will open a command line instance in a new window. Running scripts can be done in several ways, as others have mentioned running a shell within emacs works quite well, but emacs compilation mode provides some helpful functions.

Start compilation-mode with the compile function. You need to change the compile-command to be octave <<filename>>. You can also set the compile command on a per-file basis by a line similar to ## -*- compile-command: "octave test.m"; -*- as the first line in the file. I would strongly suggest creating a keybinding for compile.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.