VS Code runs MATLAB

VS Code runs MATLAB

This article realizes the configuration of VS Code to MATLAB. You can put MATLAB's command line window in VS Code and run .m files.

  • Download VS Code , needless to say
  • Install MATLAB related plug-ins. Different from the Code Runner written by some other blogs, the author recommends the following two

View Image

Matlab: This plug-in provides keyword highlighting, code snippets and code checking functions. After installation, add the following to settings.json:

"matlab.mlintpath": "D:\\MATLAB\\R2019a\\bin\\win64\\mlint.exe",

  "matlab.linterEncoding": "gb2312",

  "files.associations": {

    "*.m": "matlab"

  },

  "matlab.matlabpath": "D:\\MATLAB\\R2019a\\bin\\matlab.exe",

  "[matlab]": {

    "files.encoding": "gb2312"

  }

matlabpath and mlintpath should be set to the path of your own matlab.exe and mlint.exe. After completion, you can write MATLAB code in VS Code. To run, you need the following plug-in.

View Image

Before installing the second plug-in, make sure that you have Python 3.6 or 3.7 environment on your computer (3.8 is temporarily not possible) and add it to the path, you must have MATLAB version 2014b or higher, and you must install MATLAB Engine API for Python . Enter at the command line

cd "matlabroot\extern\engines\python" python setup.py install

Among them, matlabroot refers to the matlab installation path. You don't need to find this yourself. Enter matlabroot directly in the installed MATLAB command line to return to the path, just copy and replace.

After installation, you can open python to check whether it is installed. Enter the following command in the python command line

import matlab.engine eng = matlab.engine.start_matlab()

If there is no error, the installation was successful. You can safely install the plug-in in VS Code and it will take effect after restarting.

  • At this time, VS Code has 3 more commands:

View Image

Open a Matlab Terminal: Open a Matlab terminal, the command line

Run current Matlab Script: Run the current .m file

Run current selection in Matlab: Run the selection

These commands can be invoked by the shortcut keys Ctrl + Shift + P, or you can set the shortcut keys yourself. The operation effect is as follows:

View Image

  • Also recommend two other plugins

View Image

MatlabSnippets: provide more code hints, including plot, sin, regress and other functions

View Image

matlab-formatter: After installation, you can use Alt + Shift + F to automatically format the code, but if there are Chinese content or comments in the code, it will be garbled

 


Reference : https://blog.csdn.net/qq_42573803/article/details/104804413