VS Code configure matlab

Preface: Matlab integrates a large number of mathematical processing functions well, and even encapsulates methods including signal processing, image processing, and neural networks. However, matlab is often criticized for its shortcomings such as slow startup, no code completion, and an unfriendly development environment. Algorithm writers often need to perform a lot of repetitive actions. VS Code is a lightweight, open source, and ecologically excellent editor launched by Microsoft.
This article introduces in detail the method of using VS Code to configure the matlab environment, so that algorithm writers can use VS Code to write .m files, and make full use of the development friendliness of VS Code and the powerful functions of Matlab.

Tools/software required for this article:

  • Matlab software
  • VS Code software
  • VS Code plugin: Code Runner
  • VS Code plugin: Matlab
  • VS Code plugin: matlab-formatter
  • VS Code plugin: Matlab interactive Terminal

Overview of the steps:

  • Confirm that VS Code and matlab have been installed (configure the system variables of matlab)
  • Install plugins for VS Code
  • Configure settings.json in VS Code
  • (Optional) Implement Matlab's interactive terminal interface in VS Code, and set shortcut keys to run

Step 1: Confirm that VS Code and matlab have been installed

VS Code official website: https://code.visualstudio.com/
matlab official website: https://ww2.mathworks.cn/products/matlab.html

Matlab 2020 cracked version installation package (including WIndows version and Linux version)
link: https://pan.baidu.com/s/1LPqlPF1TTSbO5pFH126bPg?pwd=kw7a
Extraction code: kw7a

It is recommended that the installation path be in English and that system variables be configured.

Step 2: Install the plugin for VS Code

The VS Code plug-in installation steps are as follows:

  1. Click the Extensions tab on the left, or press Ctrl+Shift+X;
  2. Enter the plugin you want;
  3. Click on the plugin, Install.

Install the plugin:

  1. Matlab
  2. matlab-formatter
  3. Matlab Interactive Terminal
  4. Code Runner

insert image description here

Step 3: Configure settings.json in VS Code

(1) Set up the Matlab graphical interface and configure the Matlab path

Matlab: Linter Encoding: “gb2312” ;
Matlab: Matlabpath: “Your bin\matlab.exe installation path” ;
Matlab: Mlintpath: “Your bin\win64\mlint.exe installation path”.

insert image description hereinsert image description here
(2) Configure code-runner

Open "settings.json" and add the following configuration to the code-runner object:

"code-runner.executorMap":{
		"matlab": "cd $dir && matlab -nosplash -nodesktop -r $fileNameWithoutExt",
		
		...只需要在此添加上面该行代码即可,括号内的其他代码不用管...
}

insert image description hereinsert image description here(3) Add other configurations

Enter the four objects shown in the figure in setting.json:

    "files.associations": {
    
    
        "*.m":"matlab",
    },

    "code-runner.runInTerminal": true,

    "[matlab]" : {
    
    
        "files.encoding": "gb2312",
    },
    
    "files.autoGuessEncoding": true,

Note: After each object is defined, a comma must be added, otherwise an error will be reported

insert image description here

The functions of the four configuration objects are:

  • "files.associations" - Let VS Code recognize the .m file as a matlab file;
  • "code-runner.runInTerminal": true - let the result of code-runner be displayed in Terminal;
  • The remaining two items are encoding options. Chinese comments will inevitably appear in matlab. If matlab uses gb2312 encoding, it will not be garbled

The configuration is complete, Ctrl+S to save the settings.json file!

At this point, click "Run Code" to run the .m file in VS Code. The code runs by calling the matlab terminal, so a "Matlab Command Window" window will pop up, which is equivalent to the terminal on Matlab, and you can enter the matlab code on it to run.
insert image description hereinsert image description here

Step 4: (Optional) Implement Matlab's interactive terminal interface in VS Code, and set shortcut keys to run

Through the above configuration, VS Code can run Matlab, but the command terminal of Matlab will be selected after it needs to be run, and the interaction cannot be directly realized in VS Code. Therefore, this step describes how to configure the interactive terminal interface of Matlab.

(1) First make sure that you have Python3.6 or 3.7 on your computer

  • Enter the installation directory of matlab to the ...\extern\engines\python folder, you can see that there is setup.py in the directory
  • Open the cmd command line, enter the above directory, run the python setup.py install command, and run it several times until it succeeds
  • Enter Python and enter the following code
import matlab.engine
eng = matlab.engine.start_matlab()

insert image description here
insert image description here

  • Return to VS Code and open the settings of Matlab interactive Terminal, add the local python.exe path

insert image description here

  • After setting, restart VScode, shortcut key Ctrl+Shift+p, enter matlab and you will find three more commands:

insert image description here
insert image description here

Use the first command to open a Matlab command line terminal (open one every time you run it), you can directly enter the command, or enter the m file name (the folder where the file is already opened) to run; use the second command
to You can run the current .m file directly;
use the third command to run the currently selected text in the Matlab terminal; if no text is selected, run the current line.


(2) Set command shortcut keys

After completing the above configuration, you need to execute Ctrl+Shift+p every time you run the code, and select one of the three commands to run.
For ease of use, we can define shortcut keys for commands, as follows:

insert image description here
insert image description here

So far, you can use Matlab on your VScode beautifully!
insert image description here

Guess you like

Origin blog.csdn.net/Joker00007/article/details/127760652