Environment:
System: Kali Linux 2017
java:1.8
matlab:r2016b
idea:2016
1. Write the Matlab script:
Drawplot.m
% Paint Custom Function Drawplot parameter x y function drawplot (x, y) plot (x, y); percent percent use MATLAB function plot () Graph grid on% grid display Xlabel (' X-axis value ') ylabel (' Y-axis value ') title (' Java calls matlab graphing example ')
Script saved to the specified folder
In the MATLAB command window:
>> Deploytool
Select Liberary Compiler
Type:java Package
Exported: Select DRAWPLOT.M
Modify the class name to: Plotter
Click Package
The folder below will be more than the project, there are we want to use the jar (Xxx.jar)
New project under Idea: Javamatlab
There are two dependent packages under the Libs folder:
---xxx.jar
---javabuilder,jar (in MATLAB installation directory \toolbox\javabuilder\jar\jarbuilder.jar)
PackageCom.xh.matlab4java;ImportCom.mathworks.toolbox.javabuilder.MWArray;ImportCom.mathworks.toolbox.javabuilder.MWClassID;Importcom.mathworks.toolbox.javabuilder.MWComplexity;ImportCom.mathworks.toolbox.javabuilder.MWNumericArray;ImportPlotdemon. Plotter;/*** Created by Root on 8/25/17.*/ Public classPlottest { Public Static voidMain (string[] args) {//TODO auto-generated Method StubMwnumericarray x =NULL;//an array that holds X-valuesMwnumericarray y =NULL;//array that holds the Y valuePlotter Theplot =NULL;//instances of the plotter class (when Matlab is compiled, the new class) intn = 20;//Drawing points Try { //assigning values for x, y int[] dims = {1, n}; X=mwnumericarray.newinstance (dims, mwclassid.double, mwcomplexity.real); Y=mwnumericarray.newinstance (dims, mwclassid.double, mwcomplexity.real); //Define y = x^2 for(inti = 1; I <= N; i++) {X.set (I, I); Y.set (i, I*i); } //initializing an Plotter objectTheplot =NewPlotter (); //Drawingtheplot.drawplot (x, y); Theplot.waitforfigures (); } Catch(Exception e) {System.out.println ("Exception:" +e.tostring ()); } finally { //Releasing local resourcesMwarray.disposearray (x); Mwarray.disposearray (y); if(Theplot! =NULL) Theplot.dispose (); } }}
Attention:
Select Edit Configurations
Add a Run environment parameter
Ld_library_path=/opt/matlab/runtime/glnxa64
Otherwise you will get an error: Cannot link to libmwmclmcrrt.so.9.1
The configuration is complete and functioning properly.
Java calls MATLAB drawing