.m files as MATLAB/Octave for Sublime Text

MatlabOctaveSublimetext2Sublimetext

Matlab Problem Overview


Sublime sees .m files as Objective C files.

I've been given a bunch of MATLAB/Octave files to work with using Octave, all with .m extensions.

If I change the extensions to .matlab, then Sublime will recognize them and format them correctly, but then Octave doesn't see them.

I would like to either:

(1) alter Sublime so that it recognizes .m as MATLAB/Octave instead of Objective C, or

(2) tell Octave to recognize .matlab as something it can just run.

I've poked around on the Sublime and Octave sides of this, but to no avail (this is not the answer), and I'm an Octave noob. Thank you.

Matlab Solutions


Solution 1 - Matlab

Sublime 2 can be configured to associate certain file extensions to certain syntax highlighting schemes. See this answer for exactly how to do it https://stackoverflow.com/a/8014142/694184

Solution 2 - Matlab

Here is how to run Matlab/Octave code directly from Sublime Text with CTRL+B and associate .m files to this Matlab syntax. Here it is specific for Windows, see the reason below in the note.

First a Matlab.sublime-build:

{
    "cmd": ["cmd", "/k", "D:\\Documents\\software\\octave-4.0.0\\bin\\octave-cli.exe", "$file"],
    "selector": "source.matlab"
}

Then a Matlab.sublime-settings:

{
	"extensions":
	[
	    "m"
    ]
}

Note: I used the cmd /k trick to have the plot displayed in a new window that doesn't "disappear" after 10 milliseconds (see https://stackoverflow.com/questions/66054173/how-to-run-octave-code-without-the-octave-ide-similarly-to-python and https://stackoverflow.com/questions/52569584/octave-how-to-prevent-plot-window-from-closing-itself). For this reason, don't forget to add this to your plots:

h = plot(...)
waitfor(h)

Solution 3 - Matlab

Are those files scripts or functions? If they are Octave scripts and not functions then you can have any extension you like and use source() to run them, independently of the extension. If they are functions, unlike Matlab you can define functions in a script file, load it, and they will stay in memory.

But if you don't need syntax highlight at all, you should be able to change Sublime, just find the file Objective-C.Sublime-package. Without running it, just by looking at the files in the installer, I see two ways to do it:

  1. try to remove it (or just change its name for something .bak so you can restore it in case something goes wrong)
  2. that file is a tarball with two XML files. Open those files and edit the entry that sets what's the extension for those files for something different of .m

And of course, there's also the option of change to a free as in freedom text editor that allows you to look in their source and do whatever you want.

Solution 4 - Matlab

This worked fine for me. I had just put the path for the octave and the code is perfectly running

{
"cmd": ["/usr/local/octave/3.8.0/bin/octave-3.8.0", "$file"],
"selector": "source.m"
}

Save it as Octave.sublime-build

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionscharfmnView Question on Stackoverflow
Solution 1 - MatlabEric LeschinskiView Answer on Stackoverflow
Solution 2 - MatlabBasjView Answer on Stackoverflow
Solution 3 - MatlabcarandraugView Answer on Stackoverflow
Solution 4 - MatlabAnurag MaraviView Answer on Stackoverflow