Windows 有没有办法让现有的cmd窗口执行命令?
这就是我的处境 我正在使用Windows操作系统。我正在运行一个MatlabGUI,它在启动时启动另一个可执行文件。以批处理模式运行的其他可执行文件在后台以cmd运行 我希望这样,当用户单击MatlabGUI上的按钮时,另一个可执行文件将运行命令并保持打开状态。这可能吗Windows 有没有办法让现有的cmd窗口执行命令?,windows,matlab,terminal,cmd,Windows,Matlab,Terminal,Cmd,这就是我的处境 我正在使用Windows操作系统。我正在运行一个MatlabGUI,它在启动时启动另一个可执行文件。以批处理模式运行的其他可执行文件在后台以cmd运行 我希望这样,当用户单击MatlabGUI上的按钮时,另一个可执行文件将运行命令并保持打开状态。这可能吗 注意:我不想打开一个新的cmd窗口,我希望现有的窗口能够执行命令。不幸的是,Matlab似乎没有您想要的功能,至少不是直接的。我发现了一篇文章,解释了如何在.NET的帮助下实现这一点,这很幸运,因为你在Windows平台上: 我
注意:我不想打开一个新的cmd窗口,我希望现有的窗口能够执行命令。不幸的是,Matlab似乎没有您想要的功能,至少不是直接的。我发现了一篇文章,解释了如何在.NET的帮助下实现这一点,这很幸运,因为你在Windows平台上: 我从那篇文章中抄了很多
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提供了一些相关文档:
希望这能帮助你,或者让你开始。如果我还遗漏了什么,请告诉我。不幸的是,Matlab似乎没有你想要的能力,至少不是直接的。我发现了一篇文章,解释了如何在.NET的帮助下实现这一点,这很幸运,因为你在Windows平台上: 我从那篇文章中抄了很多
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提供了一些相关文档:
希望这能帮助你,或者让你开始。如果我还遗漏了什么,请告诉我。您可以从ansys的角度来处理。从-B-R开始阅读python脚本 从这里,您可以建立一些双向协议,例如轮询文件,或者更好地,通过从python运行web服务器 然后,您可以从matlab与正在运行的ansys实例进行通信。如果选择web服务器,则使用MATLABs urlread
使用python设置web服务器很容易,但您必须学习如何向宿主的ansys应用程序发送命令。您可以从ansys方面进行此操作。从-B-R开始阅读python脚本 从这里,您可以建立一些双向协议,例如轮询文件,或者更好地,通过从python运行web服务器 然后,您可以从matlab与正在运行的ansys实例进行通信。如果选择web服务器,则使用MATLABs urlread
使用python设置web服务器很容易,但您必须学习如何向宿主ansys应用程序发送命令。您为此做了哪些尝试?首先想到的是将数据发送到您启动的bash可执行文件的标准输入。我应该注意到,在后台运行的可执行文件在任何时候都不会成为焦点。因此,使用bash exe的标准输入是不可能的,因为用户不知道要输入的命令。你不能将标准输入重定向到另一个流吗?我想过这样做,但我不知道如何将输入重定向到命令窗口快速搜索出现了这样的结果:这可能会有所帮助。您应该能够启动命令窗口作为您创建的输出流的目标。然后,发送到输出流的任何数据都将进入命令窗口。我已经有一段时间没有在Matlab中进行文件操作了,或者我会用代码作为答案发布。你为此做了哪些尝试?首先想到的是将数据发送到您启动的bash可执行文件的标准输入。我应该注意到,在后台运行的可执行文件在任何时候都不会成为焦点。因此,使用bash exe的标准输入是不可能的,因为用户不知道要输入的命令。你不能将标准输入重定向到另一个流吗?我想过这样做,但我不知道如何将输入重定向到命令窗口快速搜索出现了这样的结果:这可能会有所帮助。你应该能够 启动命令窗口作为您创建的输出流的目标。然后,发送到输出流的任何数据都将进入命令窗口。我已经有一段时间没有在Matlab中进行文件操作了,否则我会用代码作为答案发布。