Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to call Matlab Program by Java

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "Java how to call Matlab program", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "Java how to call Matlab program" this article.

How to call Matlab Program by Java

Background

Matlab can easily handle very complex mathematical calculations, and Java has changeable application scenarios, such as Web development. This paper describes how to combine the advantages of the two. The basic idea is to package Matlab core programs into Jar for ordinary Java programs to call.

Concrete steps

1. Check that the built-in Java version of Matlab is consistent with the Java version installed on the system?

Check the built-in Java version of MATLAB

Check the Java version of the system

two。 Prepare a Matlab code to call

In order to test the use of various data types (such as the matrix data type of Matlab), this paper uses a slightly more complex Matlab test program: ranging-based network positioning. It contains multiple M files, of which the main function code is as follows. There are 7 input parameters: gCov is a matrix, and the others are scalars. There are 2 output parameters, all of which are matrices.

Function [nodeLoc, pMds] = main_localization (N, dim, space, nGps, gCov, sigma, numMiss) nodeLoc = diag (ones (dim,1) * space) * (rand (dim,N)-0.5); dltSec = zeros (NMague 1); achrIdx = 1RV nGPs; covMats = zeros (dim,dim,nGps); for n = 1:nGps covMats (:,:, n) = gCov End [CT,CR] = round_robin (nodeLoc,dltSec,sigma); [A magnificent Magi y] = gen_Ay (CT,CR,ones (N)); connMat = gen_connMat (N Magne numMiss); dltEst = est_dlt_ls (Ameme Magi connMat); distVec = ymai AvocdltEstt; distMat = diag_vec2mat (distVec); edm = distMat.^ 2 PGps = mvnrnd (nodeLoc (:, achrIdx)', gCov)'; pMds = classic_mds (edm, dim); pMds = orthogonal_procrustes (pMds, pGps, achrIdx); end

A list of other M files is shown in the following figure:

3. Package the Matlab code into a Jar package

(1) enter the deploytool instruction in the Matlab command line window to evoke the packaged deployment tool

(2) configure packaging type, package name and class name; select the file of M to be packaged

(3) waiting for the packaging to be completed, there should be 3 ticks.

(4) the structure of the project directory generated by the package is as follows

4. Create a new Java project and call the Jar package obtained by Matlab

(1) Eclipse new Java project (not to repeat)

(2) add two Jar packages to the Java project

The Jar package under the Matlab installation directory:.\ MATLAB\ R2017a\ toolbox\ javabuilder\ jar\ javabuilder.jar the Jar package generated by the previous M file:.\ localization_matlab\ for_redistribution_files_only\ localization_matlab.jar

(3) write the Jar package generated by calling Matlab in the Java program. The source code is as follows:

Package com.csrl.localization;import com.mathworks.toolbox.javabuilder.MWClassID;import com.mathworks.toolbox.javabuilder.MWException;import com.mathworks.toolbox.javabuilder.MWNumericArray;import localization_matlab.MdsLocalization;public class TestLocalization {public static void main (String [] args) {try {MdsLocalization matrixCompletion = new MdsLocalization (); double N = 10; double dim = 2; double space = 500; double sigma = 2 Double nGps = 5; double numMiss = 0; double [] [] gCovArr = {{1scot0}, {0recover1}}; MWNumericArray gCov = new MWNumericArray (gCovArr,MWClassID.DOUBLE); / / convert the two-dimensional array to a matrix / / the first parameter "2" represents the number of output parameters of the original Matlab function, followed by the input parameters of the original Mat bar function The output parameters are stored in the Object array Object [] result = matrixCompletion.main_localization (2, N, dim, space, nGps, gCov, sigma, numMiss); MWNumericArray data = (MWNumericArray) result [0]; / / the first output parameter double [] [] nodeLoc = (double [] []) data.toDoubleArray () / / convert the matrix to a two-dimensional array data = (MWNumericArray) result [1]; / / the second output parameter double [] [] pMds = (double [] []) data.toDoubleArray () / / convert matrix to two-dimensional array System.out.println (result [0]); / / same as Matlab output format, output matrix System.out.println (result [1]); System.out.println (nodeLoc [0] [0]) / / output an element in the matrix System.out.println (pMds [0] [0]) through a two-dimensional array index;} catch (MWException e) {e.printStackTrace ();}} Java calls Matlab function notes

Note is to record how to call the matlab function through java:

1. Enter deploytool on the command line

two。 Click on the part 3 marked with a red box in the image above. Select javapackage and fill in name,classname in turn

Mcc errors may occur in the process of package

4. Write the java class and test import com.mathworks.toolbox.javabuilder.MWException;import computef.ComputeF;public class matlab2javaTest {public static void main (String args []) throws MWException {System.out.println (System.getProperty ("java.library.path")); ComputeF cl = new ComputeF (); cl.computef (0);}}

The first time you run it, the following error is reported

Exception in thread "main" java.lang.UnsatisfiedLinkError: Failed to find the library mclmcrrt7_14.dll, required by MATLAB Builder JA, on java.library.path.

This library is typically installed along with MATLAB or the MCR, its absence may indicate an issue with that installation or the current path configuration.

The MCR version that this component is trying to use is: 7.14.

The reason for the error may be that there is no path to matlab in path, that is, the file mclmcrrt7_14.dll cannot be found. Just add the matlab path to path.

5. Call a function that returns a result

M file content:

Function [fmaine c] = computef (key)

When calling the computerf function

Object [] result = null;result = cl.computef (2Power0); / / the first parameter is the number of results returned, and the second parameter is the actual parameter. The above is all the content of the article "how to call the Matlab program by Java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report