基本形でsin関数を3つ、ラベル付け、グラフの保存です。
https://jp.mathworks.com/help/matlab/learn_matlab/basic-plotting-functions.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try { | |
Future<MatlabEngine> eng = MatlabEngine.startMatlabAsync(); | |
MatlabEngine ml = eng.get(); | |
ml.eval("x = 0:pi/100:2*pi;"); | |
ml.eval("y = sin(x);"); | |
ml.eval("y2 = sin(x-.25);"); | |
ml.eval("y3 = sin(x-.5);"); | |
ml.eval("plot(x, y, x, y2, x, y3);"); | |
//plotのあと軸を追加 | |
ml.eval("xlabel('x = 0:2\\pi');"); | |
ml.eval("ylabel('Sine of x');"); | |
ml.eval("title('Plot of the Sine Function','FontSize',12);"); | |
ml.eval("legend('sin(x)','sin(x-.25)','sin(x-.5)')"); | |
ml.eval("saveas(gcf,'sample.png')"); | |
ml.eval("pause(30);"); | |
} |
[注意点]
・ラベルはplotのあとに追加する
・ファイルを表示した時、関数が終了すると消える(pause関数で表示させておく)
・ファイルを保存する時、saveas(gcf,'sample.png') この時のgcfは「現在の Figure を保存するには、fig に gcf を指定します。」
https://jp.mathworks.com/help/matlab/ref/saveas.html
・JavaからMatlabを起動すると、ディレクトリも共有されていました。Matlabを開いたら、Eclipseで作成したディレクトリになっていました。グラフをそのまま保存すると、プロジェクトのホームディレクトリにそのまま保存されました。
ソースコード
https://github.com/smzn/MatlabJava04