# 北京电科院-光储直柔仿真系统设计-开发文档

# 4. Matlab嵌入

# 4.1 开发环境

  1. 在windows上安装Matlab2018a

    网上找破解版(http://www.zhanshaoyi.com/6938.html),下载完安装,注意:安装完以后去环境变量中看下是否默认在path中增加了matlab的runtime环境变量,没有的话自行增加。

  2. 将算法函数( .m文件)打成jar包

    image-20230922155058580

image-20230922155436306

image-20230916095926234

  1. 将打好的Jar包拷贝至项目路径(yatong-simulation/src/main/resources/lib)下

    image-20230922155556246

    另外,要将 matlab安装目录下的javabuilder.jar也拷贝至此处。可以用everything.exe全局搜索一下。

  2. 发布到本地maven

    1)在yatong-simulation模块下的pom.xml中增加以下代码,用以发布jar包,让项目可以正常依赖

    <build>
        <plugins>
            <!--   将自己的jar包打包发布到本地maven  -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-install-plugin</artifactId>
                <version>2.5.2</version>
                <executions>
                    <!--    Matlab算法包所依赖的环境   -->
                    <execution>
                        <id>install-javabuilder-model</id>
                        <phase>clean</phase>
                        <configuration>
                            <file>${basedir}/src/main/resources/lib/javabuilder.jar</file>
                            <repositoryLayout>default</repositoryLayout>
                            <groupId>com.yatong</groupId>
                            <artifactId>javabuilder</artifactId>
                            <version>R2018a</version>
                            <packaging>jar</packaging>
                            <generatePom>true</generatePom>
                        </configuration>
                        <goals>
                            <goal>install-file</goal>
                        </goals>
                    </execution>
    
                    <!--   Matlab算法包   -->
                    <execution>
                        <id>install-javabuilder-model</id>
                        <phase>clean</phase>
                        <configuration>
                            <file>${basedir}/src/main/resources/lib/arithmetic.jar</file>
                            <repositoryLayout>default</repositoryLayout>
                            <groupId>com.yatong</groupId>
                            <artifactId>arithmetic</artifactId>
                            <version>1.0.0</version>
                            <packaging>jar</packaging>
                            <generatePom>true</generatePom>
                        </configuration>
                        <goals>
                            <goal>install-file</goal>
                        </goals>
                    </execution>
                 <plugin>
          <plugins>
    <build>
    
    

    2)然后再pom的dependencies中,增加这两个依赖。

    <dependency>
        <groupId>com.yatong</groupId>
        <artifactId>javabuilder</artifactId>
        <version>R2018a</version>
     </dependency>
    
    <!-- 部署时使用这个依赖 算法包  -->
    <dependency>
        <groupId>com.yatong</groupId>
        <artifactId>arithmetic</artifactId>
        <version>1.0.0</version>
        <type>jar</type>
    </dependency>
    

    还有另外一种方式,在开发中提供依赖。(但不建议)

    <dependency>
      <groupId>com.effort</groupId>     <!-- 自定义即可 -->
      <artifactId>piapi</artifactId>    <!-- jar名称 -->
      <version>1.0.0</version>          <!-- 如果jar包未指定自定义即可-->
      <type>jar</type>
      <scope>system</scope>
      <systemPath>${project.basedir}/src/main/resources/lib/arithmetic.jar</systemPath>    <!-- 使用jar所在的绝对路径 -->
    </dependency>
    

    3)在IDEA中点击

    image-20230922160223172

  3. java调用Matlab(参考MatlabUtil.java)

    public void runMatlab(入参){
            try {
                MatlabHelper matlabHelper = new MatlabHelper();
                /*   ----------------------------  入参 Start ----------------------------   */
    			// blablablablablablablabla
                /*   ----------------------------  入参 End ----------------------------   */
               
                /*   ----------------------------  调用Matlab Start ----------------------------   */
                Object[] result = matlabHelper.conf_bus_load(V1, V2, V3, V4, V5, P_g1, P_pv1);     
                    MWNumericArray dataArray = (MWNumericArray)result[i];
                // 遍历Matlab的出参   
                for (int i = 0; i < result.length; i++) {
                    MWNumericArray dataArray = (MWNumericArray)result[i];
                 // 处理结果   
                }
            } catch (MWException e) {
                throw new RuntimeException(e);
            }
    }
    

# 4.2 线上环境

  1. 线上环境同样需要安装 Matlab2018a,保持版本一致

    去Matlab官网下载Linux版的Matlab2018a,安装(具体步骤在csdn上面搜)

# 4.3 注意事项

  1. 项目的运行环境需要包括matlab的Runtime运行环境(和java环境类似)。
  2. 用Matlab打包算法包时,尽量包名和类名规范化,这样可以在Java项目中引入时规避各式各样的坑点。

参考

https://blog.csdn.net/shirukai/article/details/123641588a

https://blog.csdn.net/weixin_43876729/article/details/121375871

  1. Matlab的运行时环境 ,要保证其在系统环境变量配置中,其置于path靠前的位置,以防止本机安装多matlab环境被其他环境覆盖的情况出现。

# 5. 部署

# 路径

  • 项目路径 /mnt/extra/projects/fangzhen-system

  • 前端路径 /mnt/extra/projects/fangzhen-system/web

  • 后端路径 /mnt/extra/projects/fangzhen-system/server

# 前端

将vue项目用build打包,完事儿后将dist下面的所有文件夹及文件上传至 /mnt/extra/projects/fangzhen-system/web 路径下(覆盖即可)。

# 后端

执行 /mnt/extra/projects/fangzhen-system/server 路径下的deploy.sh文件,执行命令:

bash deploy.sh