Main Content

Connect .NET Application to Running MATLAB Session

Find and Connect to MATLAB

You can connect the .NET engine to shared MATLAB® sessions that are running on the local machine. To connect to a shared MATLAB session:

  • Start MATLAB as a shared engine session, or make a running MATLAB process shared using matlab.engine.shareEngine.

  • Find the names of the MATLAB shared sessions using the MATLABEngine.FindMATLAB or MATLABEngine.FindMATLABAsync static method. For a description of these methods, see MathWorks.MATLAB.Engine.MATLABEngine.

  • Pass the string containing the name of the shared MATLAB session to the MATLABEngine.ConnectMATLAB or MATLABEngine.ConnectMATLABAsync static method. These methods connect the engine to the shared session.

If you do not specify the name of a shared MATLAB session when calling MATLABEngine.ConnectMATLAB or MATLABEngine.ConnectMATLABAsync, the engine uses the first shared MATLAB session created. If there are no shared MATLAB sessions available, the engine creates a shared MATLAB session and connects to this session.

Connect to MATLAB Synchronously

Convert the MATLAB session to a shared session by calling matlab.engine.shareEngine from MATLAB.

matlab.engine.shareEngine

Find the session and connect synchronously from .NET.

using MathWorks.MATLAB.Exceptions;
using MathWorks.MATLAB.Types;
using System;

namespace MathWorks.MATLAB.Engine.ConsoleExamples {
    public class Program {   
        public static void Main(string[] args) {
            Console.Write("Starting MATLAB... ");
            string[] names = MATLABEngine.FindMATLAB(); 
            if (names.Length == 0)
                Console.Error.WriteLine("No shared MATLAB sessions found.");
            string myMATLAB = names[0]; // Pick the first
            try {
                using (dynamic matlab = MATLABEngine.ConnectMATLAB(myMATLAB)) {
                    matlab.disp(new RunOptions(nargout: 0), "Hello, shared MATLAB.");
                }
            } catch (MATLABNotAvailableException) {
                Console.Error.WriteLine("Could not connect to MATLAB engine.");
            }

            // Call when you no longer need MATLAB Engine in your application.
            MATLABEngine.TerminateEngineClient();
        }
    }
}

Connect to MATLAB Asynchronously

Convert the MATLAB session to a shared session by calling matlab.engine.shareEngine from MATLAB.

matlab.engine.shareEngine

Find the session and connect asynchronously.

using MathWorks.MATLAB.Exceptions;
using MathWorks.MATLAB.Types;
using System.Threading;
using System.Threading.Tasks;
using System;

namespace MathWorks.MATLAB.Engine.ConsoleExamples {
    public class Program {
        static async void Main(string[] args) {
            string[] names = await MATLABEngine.FindMATLABAsync();
            if (names.Length == 0)
                Console.WriteLine("No MATLAB sessions available.");
            else {
                dynamic matlab = await MATLABEngine.ConnectMATLABAsync(names[0]);
                matlab.disp(new RunOptions(nargout: 0), "Hello, shared MATLAB.");
            }
        }
    }
}

Specify Name of Shared Session

You can specify the name of the shared MATLAB session when you execute the matlab.engine.shareEngine MATLAB function. Doing so eliminates the need to use MATLABEngine.FindMATLAB or MATLABEngine.FindMATLABAsync to find the name.

For example, start MATLAB and name the shared session myMatlabEngine.

matlab -r "matlab.engine.shareEngine('myMatlabEngine')"

Connect to the named MATLAB session.

using MathWorks.MATLAB.Types;
using System;

namespace MathWorks.MATLAB.Engine.ConsoleExamples
{
    public class Program {   
        public static void Main(string[] args) {
            using (dynamic matlab = MATLABEngine.ConnectMATLAB("myMatlabEngine")) {
            matlab.disp(new RunOptions(nargout: 0), "Hello, myMatlabEngine.");
            // Call when you no longer need MATLAB Engine in your application.
            MATLABEngine.TerminateEngineClient();
        }
    }
}

See Also

|

Related Topics