有没有办法使现有的cmd窗口执行命令?

所以这是我的情况。

我正在使用Windows操作系统。 我正在运行一个Matlab GUI,在启动时启动另一个可执行文件。 另一个可执行文件以批处理模式运行(在后台运行cmd)。

我想这样做,当用户点击Matlab GUI上的button时,其他可执行文件将运行一个命令并保持打开状态。 这可能吗?

注:我不想打开一个新的cmd窗口,我想要现有的执行命令。

不幸的是,似乎没有Matlab能够找到,至少不是直接的。 我发现一个帖子,解释如何在.NET的帮助下做到这一点,这是幸运的,因为你是在Windows平台上: http : //www.mathworks.com/matlabcentral/answers/72356-using-matlab-到发送串到最标准输入-的-另一个控制台应用程序

我从这篇文章中复制了很多

function lh = task() % Initialize the process and its StartInfo properties. % The sort command is a console application that % reads and sorts text input. process = System.Diagnostics.Process; process.StartInfo.FileName = 'sort.exe'; process.EnableRaisingEvents = true; process.StartInfo.CreateNoWindow = true; % Set UseShellExecute to false for redirection. process.StartInfo.UseShellExecute = false; %Redirect the standard output of the sort command. process.StartInfo.RedirectStandardOutput = true; % Set our event handler to asynchronousously read the sort output. lh = process.addlistener('OutputDataReceived',@sortOutputHandler); % Redirect standard input as well. This stream % is used synchronously. process.StartInfo.RedirectStandardInput =true; % Start the process. process.Start(); %Use a stream writer to synchronously write the sort input. ProcessStreamWriter = process.StandardInput; % Start the asynchronousous read of the sort output stream. process.BeginOutputReadLine(); %Prompt the user for 4 input text lines. Write each %line to the redirected input stream of the sort command. numInputLines = 0; while(numInputLines ~= 4) inputText = input('Enter a text line (or press the Enter key to stop):', 's'); numInputLines = numInputLines + 1; if(~isempty(inputText)) ProcessStreamWriter.WriteLine(inputText); end end disp('end of input stream'); %end the inputr stream to the sort command ProcessStreamWriter.Close(); % wait for the sort process to write the sorted text lines process.WaitForExit(); process.Close(); end 

为了处理来自CMD的任何输出,您需要:

 function processOutputHandler(obj,event) %collect the sort command output and print in command window if(~isempty(event.Data)) disp(event.Data); end end 

您可以使用流式写入器来同步写入排序输入。

 processStreamWriter = process.StandardInput; 

再次,我从前面提到的帖子中看到了这一点,所以我不能对代码进行任何评价,但是我确实认为它能够完成你正在寻找的东西。 不幸的是,我很确定这将完成你所需要的。 我目前没有在Windows平台上使用Matlab,或者我会测试这个。 如果您需要在MATLAB中使用.NET代码的信息(如果您需要添加一些东西来建立.NET接口,则不清楚)MathWorks提供了一些关于它的文档: http : //www.mathworks.com/help/matlab /matlab_external/using-net-from-matlab-an-overview.html

希望这有助于,或让你开始。 让我知道如果有什么我错过了。

你可以从ansys方面来解决这个问题。 用-BR开始读取一个python脚本。

从那里,你可以建立一些双向协议,例如轮询文件,或者更好地,通过从python运行一个web服务器。

然后你可以从matlab和那个正在运行的ansys实例进行通信。 如果你选择一个Web服务器,你可以使用MATLAB的urlread()。

用python建立一个web服务器很容易,但是你必须学习如何将命令发送到托管ansys应用程序。