【问题标题】:Is there a way to make an existing cmd window execute commands?有没有办法让现有的 cmd 窗口执行命令?
【发布时间】:2014-03-22 07:14:38
【问题描述】:

这就是我的情况。

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

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

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

【问题讨论】:

  • 你为此做了什么?首先想到的是向您启动的 bash 可执行文件的标准输入发送数据。
  • 我应该指出,在后台运行的可执行文件在任何时候都不会成为焦点。所以使用 bash exe 的标准输入是不可能的,因为用户不会知道要输入的命令。
  • 您不能将标准输入重定向到不同的流吗?
  • 我想过这样做,但我不知道如何将输入重定向到命令窗口
  • 快速搜索一下:stackoverflow.com/questions/15994824/… 这可能会有所帮助。您应该能够启动命令窗口作为您创建的输出流的目的地。然后,您发送到输出流的任何数据都将转到命令窗口。自从我在 Matlab 中完成文件操作以来已经有一段时间了,否则我会用代码作为答案发布。

标签: windows matlab terminal cmd


【解决方案1】:

您可以从 ansys 方面解决这个问题。以 -B-R 启动它以读取 python 脚本。

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

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

使用 python 设置网络服务器很容易,但您必须学习如何将命令分派到托管 ansys 应用程序。

【讨论】:

    【解决方案2】:

    不幸的是,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

    希望这对您有所帮助,或帮助您入门。让我知道我是否还有其他遗漏。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-15
      • 2020-10-10
      • 1970-01-01
      • 1970-01-01
      • 2020-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多