Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Matlab to Call System Function Asynchronously in background

I can use Matlab to call a system function as follows:

system('dir');

this displays the results on the standard output of matlab and runs synchronously.

I can capture the output in a variable by using:

[status,stdout]  = dos('dir');

This displays no output, but still runs synchronously.

I can run the script asynchronously by using;

system('dir &');

This pops up a command window which then runs in the foreground and remains open when the function is complete.

What I would like to do is run the command asynchronously in the background and close when it is finished. Is there a way to do this?

I'm on windows 7

like image 357
Joe Avatar asked Mar 12 '26 06:03

Joe


1 Answers

Try using the Windows start command to get better control over how the new process is launched. It will launch a new process and return. For example:

[status,out] = system('start /b /min myprogram.exe');

If the start options aren't enough for you, you can use the Java java.lang.Process or .NET System.Diagnostics.Process classes to more directly launch a process from code. Both can be called directly from Matlab code and will allow you to launch a new process, have it run asynchronously, and check in on its status. Basically, this is an alternative to Matlab's system() which will give you finer-grained control over the process you're launching. In particular, the .NET one will let you specify window state, whether to use the shell to process the command, and so on. And IIRC, the Java one is sufficient to launch a win32 console app (which I assume your exe is compiled as) without a new window appearing.

If you wanted to do some additional monitoring and interaction with the process, and are willing to write some Java code, you could run the process from a new Java worker thread in Matlab's embedded JVM.

like image 155
Andrew Janke Avatar answered Mar 14 '26 03:03

Andrew Janke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!