Main Content

Java からの MATLAB セッションの開始と終了

MATLAB® セッションを Java® プログラムから同期的または非同期的に開始することができます。次の MatlabEngine static メソッドを使用して MATLAB を起動します。

  • MatlabEngine.startMatlab — MATLAB セッションを同期的に開始します。

  • MatlabEngine.startMatlabAsync — MATLAB セッションを非同期的に開始します。

MATLAB セッションは、常にMATLAB エンジン セッションの終了のメソッドのいずれかを使用して終了しなければなりません。

MATLAB の同期的な起動

MATLAB を Java から同期的に起動します。

import  com.mathworks.engine.*;

public class StartMatlab {
    public static void main(String[] args) throws Exception {
        MatlabEngine eng = MatlabEngine.startMatlab();
        ...
        eng.close();
    }
}

MATLAB の非同期的な起動

MATLAB を Java から非同期的に起動します。返された Future オブジェクトの get メソッドを使用して、MatlabEngine オブジェクトが返されるのを待ちます。

import  com.mathworks.engine.*;
import java.util.concurrent.Future;

public class StartMatlab {
    public static void main(String[] args) throws Exception {
        Future<MatlabEngine> engFuture = MatlabEngine.startMatlabAsync();
        //Do work while MATLAB engine starts
        ...
        MatlabEngine eng = engFuture.get();
        ...
        eng.close();
    }
}

起動オプションを使用したエンジンの起動

MATLAB セッションの開始時に MATLAB 起動オプションを指定できます。MATLAB の起動オプションの詳細については、よく使われる起動オプションを参照してください。

MatlabEngine.startMatlab メソッドと MatlabEngine.startMatlabAsync メソッドは string 配列を入力として受け入れます。

MATLAB 起動オプションを使用してエンジンを同期的に起動します。

import  com.mathworks.engine.*;

public class StartMatlab {
    String[] options = {"-noFigureWindows", "-r", "cd H:"};
    public static void main(String[] args) throws Exception {
        MatlabEngine eng = MatlabEngine.startMatlab(options);
        ...
        eng.close();
    }
}

MATLAB 起動オプションを使用してエンジンを非同期的に起動します。

import  com.mathworks.engine.*;
import java.util.concurrent.Future;

public class StartMatlab {
    public static void main(String[] args) throws Exception {
        String[] options = {"-noFigureWindows", "-r", "cd H:"};
        Future<MatlabEngine> engFuture = MatlabEngine.startMatlabAsync(options);
        ...
        MatlabEngine eng = engFuture.get();
        ...
        eng.close();
    }
}

MATLAB エンジン セッションの終了

MATLAB エンジン セッションを終了するには、次の MatlabEngine メソッドのいずれかを使用します。

メソッド目的

close

Java プロセスが MATLAB セッションを既定の非共有セッションとして開始する場合、close() は MATLAB を終了します。

MATLAB セッションが共有セッションの場合、close() は MATLAB を Java プロセスから切断します。MATLAB は他に接続がなければ終了します。

disconnect, disconnectAsync

現在の MATLAB セッションから同期的または非同期的に切断します。

quit, quitAsync

現在の MATLAB セッションの強制シャットダウンを同期的または非同期的に実行します。

参考

関連するトピック