How can I invoke C++ executable file (.exe) from Matlab code or Simulink?

I am trying to run a C++ executable file (.exe) from matlab or Simulink. Currently I have to run the ++ executable file (.exe) manually and start the simulation in matlab environment to start the data exchange between matlab and C++ code. Is there any easier way to automate the process? Eg- I can just run matlab/Simulink and the C++ executable file (.exe) is invoked.
Operating system - Windows10
Matlab - 2017a/b
User Datagram Protocol (UDP) is used between matlab and C++

5 Commenti

@Rutwesh, is it in the window 10 environment or Linux? what is the protocol between matlab and C++?
.exe is Windows and not Linux, not unless a windows emulator such as Wine has been added to the Linux.
system('TheProgram.exe &')
Or use .Net System.Diagnostic.Process
@Walter, thx for your comment on .exe
@walter, Thank you for suggestion

Accedi per commentare.

 Risposta accettata

As Walter suggested you can use .Net System.Diagnostic.Process to run it.
This way you will also be able to terminate the program.
process = System.Diagnostics.Process();
process.StartInfo.FileName = 'java.exe';
process.StartInfo.Arguments = 'some args';
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = false;
process.StartInfo.CreateNoWindow = false;
process.Start();
processisrunning = ~process.HasExited;
You can then close it as follows
process.Close();
% or
process.CloseMainWindow();
% or terminate immediately
process.Kill();

4 Commenti

Thank you @ mohammad Sami
The code worked well. I used following part from your code
process = System.Diagnostics.Process();
process.StartInfo.FileName = 'C/fileadress/filename.exe';
process.Start()
This one started the exe file and simulation started but at the end of the simulation. I was not able to close the exe with the help of code. I tried following code to close
process.Close();/process.CloseMainWindow();/process.Kill();
But none of them closed the exe at the end. Please suggest a solution for this.
What is operation of following lines
process.StartInfo.Arguments = 'some args';process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = false;
process.StartInfo.CreateNoWindow = false;
Process.Kill should terminate the exe forcefully. Following is the documentation from Microsoft.
https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.process.kill?view=netcore-3.1
" The Kill method forces a termination of the process, while CloseMainWindow only requests a termination. When a process with a graphical interface is executing, its message loop is in a wait state. The message loop executes every time a Windows message is sent to the process by the operating system. Calling CloseMainWindow sends a request to close the main window, which, in a well-formed application, closes child windows and revokes all running message loops for the application. The request to exit the process by calling CloseMainWindow does not force the application to quit. The application can ask for user verification before quitting, or it can refuse to quit. To force the application to quit, use the Kill method.
The behavior of CloseMainWindow is identical to that of a user closing an application's main window using the system menu. Therefore, the request to exit the process by closing the main window does not force the application to quit immediately. "
If your exe takes arguments you can specify them with Arguments. If you wish to hide the window of your exe you can see create no window to true. You can find more information in documentation on Microsoft website.
Thank you Mohammad for the detailed explaination. The problem for was with the script I created which stopped in between due to a error and the kill statement was not execuated.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su External Language Interfaces in Centro assistenza e File Exchange

Tag

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by