How do I run MATLAB (.m) files in VSCode?

Viewed 26698

I am not able to figure out how to run MATLAB (.m) code in VSCode.

I have installed the MATLAB Extension for VSCode and added the path for the mlint file for MATLAB as stated in the extension.

"matlab.mlintpath"

VSCode identifies the file as a MATLAB file. However, the code won't run.

How can I run MATLAB code from within VSCode?

4 Answers

There is a way, this VSCode extension suggests that you can use it to run MATLAB code from within VSCode without having to open the MATLAB GUI. It does not invalidate the below though; you need to have a working, licensed MATLAB installation, which will be called in the background. Without a working, licensed MATLAB installation you cannot run MATLAB code.


As MATLAB is proprietary, there are only two ways to run MATLAB code: by running the code from the MATLAB IDE (with a valid license of course), or by compiling it to an executable file with the MATLAB compiler.
You might be able to get other IDEs to have syntax highlighting and typing-hints like MATLAB's own IDE through importing its mlint functionality, like you did, but that won't execute the code.

Thus no, you cannot execute MATLAB code directly within VSCode, you can only covertly call the MATLAB engine from inside VSCode.


A possible workaround would be to export your MATLAB code to C/C++ using the MATLAB Coder. You can then run the C/C++ code from within VSCode.

TL;DR:

I wrote Matlab Terminal Extension for VS Code on Linux. It can run matlab files and spawn a matlab-Terminal. You can find the repo here.

MatTer for VS Code

I recently wanted to used a matlab terminal in vscode. There even is an extension for this. But while using the extension I faced some flaws. Also the repository does not seem to be active anymore and the owner doesn't react to issues and similar.

So I decided to write my own first extension MatTer for VS Code and published it to the VS marketplace. It's in early Development, but it can already spawn a matlab-Terminal and run matlab files.

The repo is hosted on Github, see here: https://github.com/mauzigoe/matter-for-vscode

To install it via vscode just search for matter-for-vscode in Extension.

Critics and Feedback is welcomed, esp. since I'm new to this.

"@Liam the long discourse is necessary, because having the extension without a valid MATLAB license+installation won't work.T"

...because at some point you need at least ONE working matlab install just to develop the m-file :)

I suspect that you're right, and it comes down to a choice between embedding "foreign" code into an m-file, or making calls to the matlab engine from within a foreign IDE. They do have a free RTE but you still need some form of Matlab IDE to write in plus the Matlab Compiler toolbox to build the project out for the RTE.

The work-around is to use OS tools to remove Matlab and other proprietary tools from the loop entirely. That is why if you're going to be forced to pay $5k for a proprietary tool, it has to be justified in other ways than "it can get the job done".

VS Code extensions have the same problem as any other extension: if you look at the repository and see what is required to install and run the extension? You'll probably never install an extension again. It's about as wise as blithely downloading and installing any code from the Internet: it's no better than the source. I would wait until you figure-out how to call getOpen() etc within VS Code without downloading some random extension from some random source especially one that hasn't been updated in the past 2 years. Or at least use VS2019 Community:

https://www.mathworks.com/matlabcentral/answers/100603-how-can-i-compile-a-matlab-engine-application-using-microsoft-visual-studio-9-0-or-10-0

you could use the "Matlab Interactive Terminal" Extention, the link is below. To use it, you need to follow the instruction to install a "MATLAB Engine API for Python" and you are good to go (and you need a working Matlab).

after installing the extension, you could open the Matlab terminal in the command window (by pressing Ctrl+Shift+P and input "Open a Matlab Terminal"), and you could get the same Matlab terminal as in the real Matlab IDE, and now you could run .m code in Vscode.

Related