2014-02-21 141 views
2

所以这里是我的情况。有没有办法让现有的cmd窗口执行命令?

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

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

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

+0

你为此尝试了什么?首先想到的是将数据发送到您启动的bash可执行文件的标准输入。 – Jared

+0

我应该注意到,在后台运行的可执行文件不会在任何时候引起注意。因此,使用bash exe的标准输入是不可能的,因为用户不会知道要输入的命令。 – user3335078

+0

不能将标准输入重定向到不同的流? – Jared

回答

4

不幸的是,似乎没有Matlab能够找到,至少不是直接。我发现了一个帖子里面做解释如何与.NET的帮助下,虽然,因为你是在Windows平台上是幸运的做到这一点:http://www.mathworks.com/matlabcentral/answers/72356-using-matlab-to-send-strings-to-the-stdin-of-another-console-application

我从那个岗位复制了很多这方面

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 asynchronously 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 asynchronous 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

希望这可以帮助您,或者让您开始。让我知道是否还有其他东西我错过了。

0

您可以从ansys方面处理此问题。从-B-R开始阅读python脚本。

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

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

使用python设置web服务器很容易,但是您必须学习如何将命令发送到托管ansys应用程序。

相关问题