MATLAB: How to have the MATLAB 7.8 (R2009a) server running even after the client application that invoked it is terminated

enableserviceMATLAB

I have a client application which calls MATLAB as a Server and I am able to execute MATLAB commands from this client application.
For example, my client application is a VB .NET program:
Dim MatLab As Object
Dim Result As String
Dim out1 As Object
MatLab = CreateObject("Matlab.Application")
'Executing other MATLAB commands
Result = MatLab.Execute("surf(peaks)")
When I terminate my client application, the MATLAB Server process also terminates. Is there is way to keep the MATLAB Server process running even after the client application terminates?

Best Answer

There are a couple of ways to implement this based on your client application:
While using a Visual Basic .NET client application, modify your client program as follows:
Dim MatLab As Object
Dim Result As String
Dim out1 As Object
MatLab = CreateObject("Matlab.Application")
Matlab.Feval("enableservice", 1, out1, "AutomationServer", True)
'Executing other MATLAB commands
Result = MatLab.Execute("surf(peaks)")
In the above code, FEVAL is used to execute the MATLAB Command:
enableservice('AutomationServer',true)
which sets MATLAB's state to an Automation Server and does not terminate it when the client application terminates.
If the client application is a C/C++ program, the MATLAB Server is typically created using the MATLAB Engine API functions like engOpen( ). In this case, the following line can be added to the engine C/C++ code to prevent MATLAB from terminating even though the client application terminates:
engEvalString(ep,"enableservice('AutomationServer',true)");
The service can be closed using the following command either with FEVAL or ENGEVALSTRING depending on hoe the service was enabled:
enableservice('AutomationServer',false);