【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”时调用的文件,我们打开它
【MATLAB+VScode】Vscode运行MATLAB 2_第1张图片
除了引用外,从第一行开始分别是打开内置终端、运行当前文件、进入运行后的循环等待命令。所以我们将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 文件,其中的关键点就是执行一次代码后的循环等待命令,分析对应方法
【MATLAB+VScode】Vscode运行MATLAB 2_第2张图片
可以看到,相关方法也不过就是分析传入的command参数。但是原代码只有三种分支,一是退出(exit),二是清屏(clc),三是执行MATLAB命令,所以接下来就很容易了,只要增加一个elif来判断是否执行.m文件即可

我们不修改原始文件,而是重新创建一个文件来实现上述想法。
【MATLAB+VScode】Vscode运行MATLAB 2_第3张图片
首先我们创建一个新的文件,这里我命名为 su_script.py,代码书写如下
【MATLAB+VScode】Vscode运行MATLAB 2_第4张图片

可以看到其中红框部分就是利用正则表达式判断,如果传入的是使用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文件
在这里插入图片描述

你可能感兴趣的:(经验分享,vscode,matlab,python)