如何在MATLAB中以编程方式将命令发送到命令窗口?

问题描述 投票:3回答:1

在matlab中,我可以使用bang(!)表示法更改为另一个shell。

示例:我通过以下命令在MATLAB中输入conda环境:

!cmd '"%windir%\System32\cmd.exe" /K ""C:\Program Files\Anaconda3\Scripts\activate_<conda-env-name>.bat" "C:\Program Files\Anaconda3""'

然后我的MATLAB命令窗口显示以下内容:

(<conda-env-name>) U:\some_starting_path>

现在,有一种方法可以以编程方式将命令发送到此新输入的shell,以便该命令以shell的语法进行评估,而不是作为MATLAB命令?例如,如何编写无需手动在命令行中输入即可执行Python命令的代码?

matlab shell command-line command conda
1个回答
1
投票
不使用!命令或system()。这些是“一劳永逸”的功能。

但是您可以在Matlab中使用Java的java.lang.Process API来控制正在进行的过程并与之交互。

java.lang.Process

您可以通过function control_another_process

pb = java.lang.ProcessBuilder(["myCommand", "myArg1", "myArg2"]);
proc = pb.start;  % now proc is a java.lang.Process object
stdin = proc.getOutputStream;  % Here's how you send commands to the process
stdout = proc.getInputStream;  % And here's how you get its output
stderr = proc.getErrorStream;

% ... now do stuff with the process ...

end
或任何其他命令将其与外壳一起使用。

这里是一个Matlab类,它包装了Java代码以使其在Matlab中使用起来很方便:python

© www.soinside.com 2019 - 2024. All rights reserved.