How to Run Matlab with Visual Studio Code (vs code)

visual studio code 与matlab的配置方法

In order to have a more decent program editor for windows, I try to use vs code to write the m file for Matlab, however, I cannot run it in vs code. Then I start to search for a way to run Matlab script in vs code. Because of the updates of the vs code and related plugins, it takes me about one hour to figure out how to do this. I have read a Chinese tutorial, which is helpful but not enough. Here, I try to list the key steps for the configuration in vs code.

First, install plugins: Code runner (publisher:”Jun Han”), Matlab (publisher:”Xavier Hahn”), Matlab-formatter (publisher:”AffenWiesel”). Just choose the most popular one. Here, matlab-formatter is not necessary.

Second, modify the settings.json of vs code. the file of the setting.json is located in “C:\Users\Rocket\AppData\Roaming\Code\User\settings.json” if you install the vs code in the default directory. The modification parts are highlighted below. You may install Matlab in other folders that you need to change the related path as well.

{
    "python.pythonPath": "C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.9_3.9.1776.0_x64__qbz5n2kfra8p0\\python3.9.exe",
    "editor.renderControlCharacters": true,
    "workbench.editorAssociations": {
        "*.ipynb": "jupyter-notebook",
        "*.m": "default"
    },
    "python.languageServer": "Pylance",
    "auto-build.defaultEnv.name": "STM32F103RET6_creality",
    "security.workspace.trust.untrustedFiles": "open",
    "python.defaultInterpreterPath": "C:\\Program Files\\Python39\\python.exe",
    "diffEditor.renderSideBySide": false,
    "matlab.matlabpath": "C:\\Program Files\\MATLAB\\R2021a\\bin\\matlab.exe",
    "matlab.mlintpath": "C:\\Program Files\\MATLAB\\R2021a\\bin\\win64\\mlint.exe",
    "matlab.linterEncoding": "utf8",
    "code-runner.runInTerminal": true,
    "code-runner.executorMapByFileExtension": {
        ".m" :"cd $dir && matlab -nosplash -nodesktop -r $fileNameWithoutExt",
        ".vb": "cd $dir && vbc /nologo $fileName && $dir$fileNameWithoutExt",
        ".vbs": "cscript //Nologo",
        ".scala": "scala",
        ".jl": "julia",
        ".cr": "crystal",
        ".ml": "ocaml",
        ".exs": "elixir",
        ".hx": "haxe --cwd $dirWithoutTrailingSlash --run $fileNameWithoutExt",
        ".rkt": "racket",
        ".scm": "csi -script",
        ".ahk": "autohotkey",
        ".au3": "autoit3",
        ".kt": "cd $dir && kotlinc $fileName -include-runtime -d $fileNameWithoutExt.jar && java -jar $fileNameWithoutExt.jar",
        ".kts": "kotlinc -script",
        ".dart": "dart",
        ".pas": "cd $dir && fpc $fileName && $dir$fileNameWithoutExt",
        ".pp": "cd $dir && fpc $fileName && $dir$fileNameWithoutExt",
        ".d": "cd $dir && dmd $fileName && $dir$fileNameWithoutExt",
        ".hs": "runhaskell",
        ".nim": "nim compile --verbosity:0 --hints:off --run",
        ".csproj": "dotnet run --project",
        ".fsproj": "dotnet run --project",
        ".lisp": "sbcl --script",
        ".kit": "kitc --run",
        ".v": "v run",
        ".vsh": "v run",
        ".sass": "sass --style expanded",
        ".cu": "cd $dir && nvcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt"
    }
}

The third step, save the JSON files and try to run the Matlab file in vs code.

To use matlab-formatter, you need to right-click the open m file and choose the command ” format document”.

the running window is as follows:

The drawback of this method is that the running result will show up in a new window rather than in the terminal of vs code. And also be careful of the name of the running script, the program (Matlab ) may run the script with the same name but in a different folder.

after using this several times, it is not convenient. it is better to editor the script with vs code but run the script in Matlab GUI, then no need to close the pop-up windows. The MathWorks company should do something, even the live script is developed, but not convenient to use.

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注




Enter Captcha Here :

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据