【MATLAB+VScode】Vscode运行MATLAB 2

上一篇博客介绍了三种在VScode中编辑运行MATLAB文件的方法,但是第一种仍需打开MATLAB编辑器,并频繁切换比较费时,第二种调用MATLAB Command打开仍然较慢,第三种每次都需要Ctrl+Shift+P来运行(设置快捷键还需要注意不和其他快捷键冲突,而且不是常用的运行快捷键,容易忘记),因此考虑能否将第二种和第三种结合,实现Code Runner调用Python来运行MATLAB并在内置终端中显示结果。

本来想要看看能否在Python文件上下手,但是找了半天找不到Python文件的位置,但看了一个文章豁然开朗,这里贴出该文章

在Windows cmd下启动matlab command_作者:QAQ_5df0

===============================================================================================
首先找到这个文件夹“C:\Users\你的用户名\.vscode\extensions\apommel.matlab-interactive-terminal-0.4.0\interfaces\unicode”,其中有个 ml_script.py 文件就是执行“Run current MATLAB Script”时调用的文件,我们打开它
在这里插入图片描述
除了引用外,从第一行开始分别是打开内置终端、运行当前文件、进入运行后的循环等待命令。所以我们将Code Runner的配置修改如下,即可右键Run Code方式运行MATLAB文件,并在内置终端中输出结果。
在这里插入图片描述
其中的Python解释器位置、“ml_script.py”文件位置要替换成自己的真实位置。

	"matlab": "C:\\Python\\Python37\\python.exe C:\\Users\\17112\\.vscode\\extensions\\apommel.matlab-interactive-terminal-0.4.0\\interfaces\\unicode\\ml_script.py"

因为这是相当于在终端中使用Python解释器运行Python文件,运行过一次之后就进入了Python命令行,就不能够再这样执行命令了,导致这种方式只能运行一次,然后需要关掉终端才能再这样做。如图
在这里插入图片描述
这怎么能行,这样反而需要重复打开终端,导致效率更低。因此考虑从 ml_script.py 文件上下手。

分析 ml_script.py 文件,其中的关键点就是执行一次代码后的循环等待命令,分析对应方法
在这里插入图片描述
可以看到,相关方法也不过就是分析传入的command参数。但是原代码只有三种分支,一是退出(exit),二是清屏(clc),三是执行MATLAB命令,所以接下来就很容易了,只要增加一个elif来判断是否执行.m文件即可

我们不修改原始文件,而是重新创建一个文件来实现上述想法。
在这里插入图片描述
首先我们创建一个新的文件,这里我命名为 su_script.py,代码书写如下
在这里插入图片描述

可以看到其中红框部分就是利用正则表达式判断,如果传入的是使用Python执行.m文件的命令,则执行 run_script方法。

相关代码如下,需要的可以自行复制

from matlab_interface import MatlabInterface
import sys
import os
import re
from io import StringIO
import matlab.engine as me
from matlab.engine import RejectedExecutionError as MatlabTerminated

matlab = MatlabInterface()
matlab.run_script(sys.argv[1])
while 1:
    print('>>> ', end='')
    command = input()
    if command == "exit" or command == "exit()":
        break
    elif command == "clc" or command == "clc()":
        if os.name == 'nt':
            os.system('cls')
        else:
            os.system('clear')
    elif len(re.findall('.*?\.py ".*?"', command)) > 0:
        file = re.findall('.*?\.py "(.*?)"', command)[0]
        matlab.run_script(sys.argv[1])
    else:
        try:
            stream = StringIO()
            err_stream = StringIO()
            matlab.eng.eval(command, nargout=0, stdout=stream, stderr=err_stream)
            print(stream.getvalue())
        except MatlabTerminated:
            print(stream.getvalue(), err_stream.getvalue(), sep="\n")
            print("MATLAB process terminated.")
            print("Restarting MATLAB Engine for Python...")
            matlab.eng = me.start_matlab()
            print("Restarted MATLAB process.")
        except:  # The other exceptions are handled by Matlab
            print(stream.getvalue(), err_stream.getvalue(), sep="\n")

然后修改Code Runner配置如下
在这里插入图片描述
即将最后面的 **.py 替换成自己创建的 py文件。

随后就可以看到,终端中可以重复执行.m文件
在这里插入图片描述

  • 11
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论
回答: 要在VScode运行Matlab代码,你需要按照以下步骤进行设置。首先,你需要在VScode中安装Matlab插件。然后,你需要在VScode的设置中编辑setting.json文件,将Matlab的安装路径和Python的安装路径添加到相应的字段中。具体来说,你需要在setting.json中插入以下代码: "matlab.mlintpath" : "(matlab安装路径)\\R2021a\bin\\win64\\mlint.exe", "matlab-formatter.formatterPath": "(matlab安装路径)\\R2021a\bin\\matlab.exe", "matlab-formatter.pythonPath": "(python安装路径)\\Python38\\python.exe", "matlab-interactive-terminal.pythonPath": "(python安装路径)\\Python38\\python.exe" 请确保将上述代码中的(matlab安装路径)和(python安装路径)替换为你自己的实际安装路径。此外,你还需要安装Python 3.8,并将其路径添加到上述代码中。你可以通过提供的链接下载Python 3.8,并按照链接中的安装步骤进行安装。完成这些设置后,你就可以在VScode运行Matlab代码了。 #### 引用[.reference_title] - *1* *2* *3* [【MATLABvscode编辑代码的相关设置,保姆级教学】](https://blog.csdn.net/zty8788/article/details/125556489)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@苏丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值