2

So here is my situtation.

I am using the Windows OS. I am running a Matlab GUI that launches another executable at startup. The other executable runs in batch mode (runs in cmd in the background).

I want to make it so when a user clicks a button on the Matlab GUI, the other executable will run a command and remain open. Is this possible?

NOTE: I do not want to open a new cmd window, I want the existing one to execute commands.

  • What have you tried for this? What comes to mind first is sending data to the standard input of the bash executable you launched. – Jared Feb 21 '14 at 01:23
  • I should of noted that the executable running in the background will not be brought to focus at any point. So using the standard input of the bash exe is out of the question because the users will not know the commands to type in. – user3335078 Feb 21 '14 at 01:32
  • Cant you redirect the standard input to a different stream? – Jared Feb 21 '14 at 01:33
  • I thought about doing that, but I do not know how to redirect input to a command window – user3335078 Feb 21 '14 at 01:41
  • A quick search turned up this: http://stackoverflow.com/questions/15994824/faking-standard-input-on-the-windows-command-line It may be of assistance. You should be able to launch the command window as the destination for an output stream you create. Then any data you send to the output stream will go to the command window. Its been a while since I've done file operations in Matlab or I would post as an answer with code. – Jared Feb 21 '14 at 01:45
  • I looked at the link and their example launches a new instance of the .exe file every time they call their script. Your idea sounds good, but I am not sure how to launch the command window as the destination for the output stream using >, <, or |. If it helps, the command to open my .exe file in batch mode is `AnsysFW.exe -B` – user3335078 Feb 21 '14 at 03:18

2 Answers2

4

Unfortunately it does not appear that Matlab has the ability you are looking for, at least not directly. I found a post which does explain how to do it with the help of .NET though, which is fortunate since you are on the Windows platform: http://www.mathworks.com/matlabcentral/answers/72356-using-matlab-to-send-strings-to-the-stdin-of-another-console-application

I have copied a lot of this from that post

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

For handling any output from the CMD you need:

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

You can use a stream writer to synchronously write the sort input.

processStreamWriter = process.StandardInput;

Again, I have taken this from the previously mentioned post so I can't take any credit for the code, but I do think it will be able to accomplish what you are looking for. Unfortunately, I am pretty sure this will accomplish what you need. I don't have Matlab on a Windows platform at the moment or I would test this. If you need information on using .NET code in MATLAB (its not immediately clear if you need to add some stuff to establish the .NET interface) MathWorks provides some documentation on it: http://www.mathworks.com/help/matlab/matlab_external/using-net-from-matlab-an-overview.html

Hopefully this helps, or gets you started. Let me know if there's anything else I missed.

Jared
  • 1,449
  • 2
  • 19
  • 40
0

You can approach this from the ansys side. Start it with -B-R to read a python script.

From there, you can establish some two-way protocol, for example polling files or, better, by running a web server from python.

Then you can communicate from matlab with that running instance of ansys. If you opt for a web server, you use MATLABs urlread().

Setting up a web-server with python is easy, but you have to learn how to dispatch commands to the hosting ansys application.

Wolfgang Kuehn
  • 12,206
  • 2
  • 33
  • 46