vscode-matlab
vscode-matlab copied to clipboard
Suggestion: execute .m files
Thanks for developing this extension!
To my limited knowledge - I'm a newbie to VScode and this extension and will be happy to be told better - .m files cannot be executed at this point. (There is a pertinent suggestion in the code runner extension labeled with 'help needed'.)
I think it would be valuable for VSCode to have this feature.
Definitely something that I'll look into, thanks for the idea.
Great - thanks! I am not familiar with json but I use matlab a lot. Will be happy to support you.
@Jeff-Wolf Do you have any idea if there is already an open source extension that does this for matlab? Or know of a way to execute and debug Matlab code outside of the Matlab editor itself?
The one I know and work with is the script package for Atom. It does not allow to debug the Matlab code though.
Octave is open source and almost completely compatible with Matlab. The latest distributions come with a GUI and include a built-in debugger. This could be another reference to look into.
OK quickly checked the script package for atom, all it does is calling the Matlab executable and getting back the output to display in the editor. Would that be sufficiently interesting as a feature?
Octave is a complete reimplementation of Matlab, so they have their own debugger and everything, if I would use Octave for debugging I would force all users to install Octave, which I don't think is a very good idea.
It would definitely meet my requirements. I still use the native Matlab IDE for most of my code development but like to run and make minor adjustments in environments which can also execute tex, R, etc. and have a smooth git integration.
I agree with your point regarding Octave. I installed it at some point but I doubt most Matlab users have it.
Hi @Gimly , Thanks for this amazing plugin and @Jeff-Wolf, I mostly use MATLAB IDE in windows environment but I really liked the way it works under Linux. In Linux, one can start the Matlab command window in the VS code IDE terminal and debug /run '.m' file in the terminal itself.
On the other hand, Matclipse plugin is one of the examples that enables Matlab integration with IDE. This eclipse plugin calls a remote instance of Matlab and queries the workspace variables and shows up the in the eclipse pannel. https://github.com/krystophny/matclipse
Thanks for pointing this out @ragavsathish I am not working with Matclipse and just saw that it requires to install several eclipse components. Same problem as for Octave might apply...
@Gimly Maybe it could start with a feature like script for Atom offers and then the debugger comes next? (I have no idea about how long it takes to implement these features.)
Yeah, Matclipse looks quite interesting but it's a huge work. Maybe something to think in the long run, I'd be interested in finding resources directly from matlab about running an external debugger.
@ragavsathish I don't understand, you say that on Linux you can open the terminal in VS Code and run matlab directly from it, but what's blocking from doing the same on Windows?
@Gimly At least in Windows 7, whenever I open Matlab in PowerShell or command prompt, it opens up a new command window (possibly JVM based ) like below. Hence I need to alternate between the windows, However, in Linux the Matlab command editor is docked in VS CODE IDE which is a lot easier to work with.

@ragavsathish Could you try doing this:
matlab -nodisplay -nosplash -nodesktop -r "run('C:\your\path\mfile.m');"
@Gimly launching MATLAB from the command line is by design fundamentally different on Windows and Linux/Mac. Where the Linux/Mac versions support the -nodesktop option and will run natively in the terminal, the Windows version does not support the -nodesktop option, and will instead launch a command window rather than continue execution in the terminal. It's not necessarily feature-breaking, but it can certainly get annoying.
A related option for Windows could possibly be MATLAB's COM Automation Server or possibly MATLAB's external engine APIs, though I'm not familiar enough with either to determine whether or not they're useful in this situation.
@sco1 Thanks a lot for those information, very useful! The fact that the Windows version doesn't support the -nodesktop option is pretty annoying. Maybe we can live with the java window for the first iteration though?
Going through the COM automation server could be interesting and allow for the debugging as well, but it's a Windows only option then, quite annoying for a multiplatform extension. Using the API is also an interesting option, but quite the challenge.
Maybe just fork extension for Windows and Linux/Mac, since they're so different?
One might be able to use the API engine using a ready-made Matlab kernel for Jupyter, and then use the Jupyter extension to run through vs code. This might be simpler than linking to the API from scratch?
For Jupyter extension, see here: https://marketplace.visualstudio.com/items?itemName=donjayamanne.jupyter
For a Matlab kernel for Jupyter, see here: https://github.com/imatlab/imatlab
Have there been any progress on this issue now ?
@OsirisTerje Not really, it's pretty locked by the fact that we can't execute code through the command line on Windows without passing through the Java engine, so it cannot be ran correctly on VSCode's command line. Using the COM Automation would be a Windows only solution, which isn't really acceptable. The external engine API seems like a huge challenge...
In the end I'm not sure what would be the best approach for tackling this, maybe there is something we have overlooked?
MATLAB offers a Python API - might this help? https://de.mathworks.com/help/matlab/programming-interfaces-for-external-languages.html
@Gimly Maybe it can be implemented through this way, though I'm not quite sure about it: MATLAB can use the Java® debugger to do it. The Java debugger uses the default portnumber value 4444 to communicate with MATLAB (help from: https://ww2.mathworks.cn/help/matlab/ref/matlabwindows.html). And we know VS Code has good debug support for Java.
I have a (somewhat hacky) solution to run .m files in an open Matlab session on Windows.
It involves an additional extension in VSCode and having to run AutoHotKey, but this way I am able to
| Action | Shortcut |
|---|---|
| Pull the Command Window to the foreground by pressing | Ctrl+m |
| Run the currently open file by pressing | Ctrl+Shift+Alt+m |
| Run the current line by pressing (when no text is selected) | Ctrl+Shift+m |
| Run the current selection by pressing | Ctrl+shift+m |
| Run the current section by pressing | Ctrl+Alt+m |
If there is interest I can write a small guide on how to setup this (including the AutoHotKey script).
@rick3rt I would love to have this in my VSCode. This would make my workflow much more efficient :)
@GitDor allright, I created a repo with the guide. Hope I didn't miss a step... https://github.com/rick3rt/vscode-run-matlab
Let me know what you think!
@GitDor allright, I created a repo with the guide. Hope I didn't miss a step... https://github.com/rick3rt/vscode-run-matlab
Let me know what you think!
This is amazing! I slightly modified the auto hotkey mode so that matlab runs using F5 and F9 (for parts of code), which is what happens with Matlab editor. To preserve Vs code beahaviour with other kind of files, it is possible to allow these shortcuts only if the active tab contains ".m -".
SetTitleMatchMode, 2 ;2 means A window's title can contain WinTitle anywhere inside it to be a match
#ifWinActive, .m - ahk_exe Code.exe, ; window's title contain ".m -" and process is "Code.exe"
F5::ExecFileMatlab()
F9::CopyExecMatlab()