Detailed records of java and python calling matlab programs

Background

  • Regarding Java and Python calling matlab programs, there are many related articles on the Internet, but the quality is not good, and some prerequisites are not introduced. If you follow his steps, he succeeds, but you fail. You are very confused.
  • I think it is difficult for us to search for something now because some people write irresponsible articles. We have to spend a lot of energy to filter. There is no way. In order to avoid secondary screening, I record the successful implementation method after screening.
  • The matlab program is huge, and it costs 20 to 30 gigabytes. It is really time-consuming to toss, so I didn't crack it myself here, and directly bought an account on tb for 30 oceans.
  • My implementation here is to package the matlab program (.m file) into a jar package for java calls, and package it into a python package for python calls.
  • The outline of this article is divided into three parts. Before introducing the packaging of jar and python packages, the prerequisite environment conditions will be introduced first.

Information sharing: This time matlab related installation program download , extraction code: king

The first part, matlab related knowledge introduction

1. First of all, you must have a matlab environment

  • If you want to compile and package jar or python packages, you need to install matlab software, as shown in the figure below to download the matlab version you need, download address: https://ww2.mathworks.cn/downloads/ , but you need to log in first, the account I bought here, I won’t say much about other situations here, just follow the prompts for specific installation, and I won’t go into details. From Figure 2 below, we can see that the program is very large, and it needs to download 17G online and install about 30G , the Linux environment will be smaller, but it is almost the same. The installation time depends on your computer performance and network conditions. I installed it for about three hours.



  • If you just want to run the jar package or python package packaged by others, then you only need to install the matlab operating environment MCR, mcr download address: https://ww2.mathworks.cn/products/compiler/matlab-runtime.html
  • windows is an exe program, just follow the prompts to install it.
  • Under linux is a zip compressed package, the specific installation steps are as follows:

  • mcr installation and configuration under linux
# install dependencies
yum -y  install libXt

# decompress
mkdir matlab-mcr
unzip MCR_R2018b_glnxa64_installer.zip -d matlab-mcr/

# Install
cd matlab-mcr/
./install -mode silent -agreeToLicense yes

# Configure environment variables, remember to change the path to your own installation path, after the installation is successful, it will be printed out, prompting you to configure environment variables
tee -a /etc/profile <<-'EOF'

# matlab-mcr
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/MATLAB/MATLAB_Runtime/v911/runtime/glnxa64:/usr/local/MATLAB/MATLAB_Runtime/v911/bin/glnxa64:/usr/local/MATLAB/MATLAB_Runtime/v911/sys/os/glnxa64:/usr/local/MATLAB/MATLAB_Runtime/v911/extern/bin/glnxa64
EOF

source /etc/profile


The second part, matlab packaging jar and java program call demo

1. First of all, you must have a jdk environment.

  • You need the jdk version of your local machine to be consistent with the built-in jdk version of matlab, as shown in the figure below, mine is jdk1.8

2. Compile and package jar

  • Test program add.m, this method accepts two parameters and returns their sum.
function c = add(a,b)  
   c = a+b;  
end  



  • Calling in Java also requires matlab-related dependency library javabuilder.jar, where the jar package is located: ${matlabroot}/toolbox/javabuilder/jar/

3. The java program calls the demo

  • Please inquire about the way java imports the jar package. I won’t go into details here. I uploaded it to our maven private server here, and just quote the dependencies directly.


  • java call demo

Note: The first parameter of the add method is the number of output parameters, followed by the input parameters.

package com.cloudansys;

import com.mathworks.toolbox.javabuilder.MWException;
import com.yl.testmatlabfunc.AddFunc;

public class TestMatlab {

    public static void main(String[] args) throws MWException {
        System.out.println(System.currentTimeMillis());
        AddFunc addFunc = new AddFunc();
        System.out.println(System.currentTimeMillis());
        int a = 1, b = 2;
        Object[] objects = addFunc.add(1, a, b);
        System.out.println(System.currentTimeMillis());
        System.out.println(objects[0].toString());
    }
}

The third part, matlab package python package and python program call demo

1. First, you need to confirm the correspondence between matlab and python

2. Package python



  • Execute the installation command after entering the shell in the current directory
python3 setup.py install

3. python calls the demo

  • test_matlab.py
import add


def main():
    """main function"""
    matlab_func = add.initialize()
    res = matlab_func.add(1, 2)
    print(res)
    matlab_func.terminate()


if __name__ == '__main__':
    main()

Tags: Python Java Linux MATLAB python3

Posted by mikebr on Wed, 28 Dec 2022 03:31:29 +0300