17

I have been editing my .m files in Sublime Text 2 (henceforth referred to as Sublime for the remainder of my question) for quite some time now, and having recently become accustomed to using the build function in Sublime for running Python code, I was trying to setup something similar for MATLAB files. I am attempting to do this because I am tired of running my code and editing my code in separate windows.

Following the documentation, I tried using this very reasonable seeming code below, where the '-r' flag launches MATLAB to run my file, the "-nodesktop" flag launches MATLAB without the big hulking Java GUI, and the "-nosplash" flag launches MATLAB without the unnecessary splash screen. The selector allows for Sublime to automatically use this build system on m files, and the path variable sets the path to the directory that will launch MATLAB (directory found by running "which matlab").

I am aware that I can set an preferred text editor from within the MATLAB GUI, but I am not interested in doing that. I am interested in using Sublime as a build system in addition to a text editor.

{
    "cmd": ["matlab", "-r", "-nodesktop", "-nosplash", "$file_name"],
    "selector": "source.m",
    "path": "/usr/local/MATLAB/R2012a/bin"    
}

Upon running build on my errorless .m file, I find this error below, which doesn't make sense. I don't think it makes sense because have launched MATLAB with the standard command line options, and am receiving MATLAB internal errors, too.

/usr/local/MATLAB/R2012a/bin/matlab: 1: /usr/local/MATLAB/R2012a/bin/matlab: awk: not found
/usr/local/MATLAB/R2012a/bin/matlab: 1: /usr/local/MATLAB/R2012a/bin/matlab: expr: not found

Internal error 2: Could not determine the path of the
              MATLAB root directory.

                  original command path = /usr/local/MATLAB/R2012a/bin/matlab
                  current  command path = /usr/local/MATLAB/R2012a/bin/matlab

                  Please contact:

                      MathWorks Technical Support

                  for further assistance.

[Finished in 0.1s with exit code 1]

Has anybody else had problems like this invoking MATLAB scripts via an alternate build system? I have not tried to set up a build system for MATLAB in Sublime on any other operating system besides Ubuntu 12.04, if that information is useful.

It works now! Here is my Matlab.sublime-build:

{
    "cmd": ["/usr/local/MATLAB/R2012a/bin/matlab", "-nosplash", "<$file_name"],
    "selector": "source.m"
}

Update: I'm now hosting Sublime Text 2 R2012a build files for Linux and for Mac on my Github page as examples: https://github.com/jessebikman/Sublime-Text-2-build-Matlab-code

JesseBikman
  • 632
  • 1
  • 5
  • 22
  • perhaps you can look at how others do it with Vim: [Vim and matlab GUI - Emulate matlab Run () with Vim](http://stackoverflow.com/q/10250367/97160), [Run matlab script](http://vim.wikia.com/wiki/Run_matlab_script). If you were on [Windows](http://vim.wikia.com/wiki/Run_Matlab_script_under_Windows), I'd recommend the COM interface to interact with MATLAB – Amro Aug 08 '12 at 17:28
  • This is a great question! Are you looking to completely divorce yourself from the Matlab IDE? Or would something more along these lines work? http://blogs.mathworks.com/community/2009/09/14/matlab-emacs-integration-is-back/ – slayton Aug 08 '12 at 17:46
  • I want to completely divorce myself from the Matlab IDE. For one thing, Sublime is easier on the eyes. – JesseBikman Aug 09 '12 at 19:51
  • You should change selector value to "source.matlab" - that way when you press ctrl+b sublime will use matlab, when you have "Tools/Build system/Automatic" turned on – SuitUp Oct 22 '12 at 16:31

4 Answers4

3

I am a sublime newbie, but this worked for me. I believe you have two problems:

  1. Figure out how to set the PATH environment (that Sublime sees) to be that of the unix command prompt. On a mac, I had to setup a file called environment.plist and add the path there. This should get rid of your awk and expr errors.

  2. Here is my matlab.sublime-build:

    { "cmd": ["/Applications/MATLAB_R2011b.app/bin/matlab","<$file_name"] }

Note the < just before $file_name.

This isn't perfect: adding the nodesktop and nosplash options lead to errors. As the build script stands now, the program runs fine in the terminal, but I still get the splash.

JesseBikman
  • 632
  • 1
  • 5
  • 22
Rob
  • 46
  • 2
  • Ok, so now I have `{ "cmd": ["/usr/local/MATLAB/R2012a/bin/matlab","-nosplash", "-r", "<$file_name"], "selector": "source.m" } ` for a build file. It successfully starts up Matlab in nodisplay mode, and doesn't display the splash screen, but as soon as Matlab finishes loading my startup.m file, Sublime interprets that as `[Finished in 8.4s]`. This is a normal startup time for me, but it's still odd that the script doesn't run. – JesseBikman Aug 10 '12 at 13:58
  • yeah you are right. it doesn't execute the script. my cobbled together command line works fine from the unix prompt, but this works for me **now** inside of sublime: ` { "cmd": ["/Applications/MATLAB_R2011b.app/bin/matlab", "-nosplash", "<$file_name"] } ' -- get rid of "-r" – Rob Aug 10 '12 at 15:02
2

just tried to get my Matlab code compiled through Sublime. Plugged in your sublime-build snippets and it works, thanks for that! However, I usually code computer vision related stuff where you kinda do this a lot:

    img = doImageConversion();
    imshow(img);
    pause;

    img = doAnotherImageConversion();
    imshow(img);
    pause;

The issue here is that sublime does not halt on pause instructions. You may overcome this by replacing the pauses by pause(3). Not quite satisfying though! To make the build actually halt I found these Matlab.sublime-build settings pretty handy:

{
    "cmd": ["xterm", "-e", "matlab", "-nosplash", "-nodesktop", "-r", "run $file_name; quit;"],
    "selector": "source.m"
}

This requires xterm to be installed on your linux distribution. Now Sublime will start the xterminal running the Matlab file over there, actually halt on pause instructions and quit in the end. I didn't test this snippet on older Matlab versions.

Cheers lustiz

lustiz
  • 21
  • 1
  • Hey cool! Fork me on Github and see if it’s better than what’s already been contributed. I’ve kind of left this alone for a few months and it seems to be working out. – JesseBikman Dec 15 '12 at 23:56
1

Just a guess - I haven't used the build system in SublimeText - but you might need to put the -r option immediately before the filename. If you run matlab -r -nodesktop "mycommand", it starts up normally, tries to execute a file nodesktop, and fails.

The MATLAB installation path also (typically, unless you've installed it differently) has a lower-case r in r2012a. Maybe that's an issue.

The last thing I could suggest is to also use the -sd option, which specifies the MATLAB startup directory.

Hope those thoughts help!

Sam Roberts
  • 23,951
  • 1
  • 40
  • 64
  • When you run 'matlab -r -nodesktop "mycommand"', what is the error message? I copied the path shown here from running "which matlab", and the error message compares the original command path to the current command path and they are identical. I'll try switching the '-r' around, adding the '-sd' switch, and maybe even using '-nojvm'. Will update later. – JesseBikman Aug 09 '12 at 19:49
  • I switched the '-r' around to no avail. The '-sd' switch is exclusive to Windows. Using the '-nojvm' switch didn't help either. – JesseBikman Aug 09 '12 at 20:48
0

My solution is much based on yours, but I added the try catch statement to avoid open matlab several times without closing it after an error, I created the command as:

{
"cmd": ["/Applications/MATLAB_R2014b.app/bin/matlab","-nosplash", "-nojvm", "-r", "cd $file_dir; try run $file_name; catch varException; display(varException); end; quit;"], "selector": "source.m"
}   

And if you are eager to understand the errors, use this other one with just the most recent stack printing of errors:

{
"cmd": ["/Applications/MATLAB_R2014b.app/bin/matlab","-nosplash", "-nojvm", "-r", "cd $file_dir; try run $file_name; catch varException; display(varException.message);display(varException.cause);display(varException.stack(1)); end; quit;"], "selector": "source.m"
}

Finally, if you are really into understanding all the errors, use this final version with full stack printing of errors:

{
    "cmd": ["/Applications/MATLAB_R2014b.app/bin/matlab","-nosplash", "-nojvm", "-r", "cd $file_dir; try run $file_name; catch varException; display(varException.message); display(varException.cause);for i = length(varException.stack):-1:1; display(varException.stack(i)); end; end; quit;"], "selector": "source.m"
}