Java calls MATLAB drawing

Source: Internet
Author: User
Tags kali linux

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.