{"id":295591,"date":"2024-05-20T15:39:29","date_gmt":"2024-05-20T07:39:29","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/295591.html"},"modified":"2024-05-20T15:39:41","modified_gmt":"2024-05-20T07:39:41","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-java-%e4%b8%ad%e8%b0%83%e7%94%a8-matlab","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/295591.html","title":{"rendered":"\u5982\u4f55\u5728 Java \u4e2d\u8c03\u7528 Matlab"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27142930\/cf4e3970-ddcb-4f22-8234-91d05041f4fb.webp\" alt=\"\u5982\u4f55\u5728 Java \u4e2d\u8c03\u7528 Matlab\" \/><\/p>\n<p><p><strong>\u5728Java\u4e2d\u8c03\u7528Matlab<\/strong>\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u4e0d\u540c\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\uff1a\u4f7f\u7528MATLAB Compiler SDK\u63d0\u4f9b\u7684API\u3001\u4f7f\u7528JMI\uff08Java MATLAB Interface\uff09\u63a5\u53e3\u3001\u6216\u8005\u901a\u8fc7\u547d\u4ee4\u884c\u4ea4\u4e92\u3002<strong>\u5176\u4e2d\uff0c\u6700\u5b98\u65b9\u548c\u6700\u5e38\u4f7f\u7528\u7684\u65b9\u5f0f\u662f\u501f\u52a9MATLAB Compiler SDK<\/strong>\uff0c\u5b83\u53ef\u4ee5\u5c06Matlab\u4ee3\u7801\u7f16\u8bd1\u6210Java\u7c7b\uff0c\u7136\u540e\u5728Java\u4ee3\u7801\u4e2d\u76f4\u63a5\u8c03\u7528\u8fd9\u4e9b\u7c7b\u5c31\u5982\u540c\u8c03\u7528Java\u4e2d\u7684\u539f\u751f\u7c7b\u4e00\u6837\u7b80\u5355\u3002<\/p>\n<\/p>\n<p><p>\u66f4\u8be6\u7ec6\u6765\u8bf4\uff0cMATLAB Compiler SDK\u5305\u542b\u4e00\u4e2a\u540d\u4e3aMATLAB Runtime\u7684\u7ec4\u4ef6\uff0c\u8fd9\u662f\u4e00\u4e2a\u72ec\u7acb\u4e8eMATLAB\u7684\u8fd0\u884c\u65f6\u5e93\uff0c\u5b83\u5141\u8bb8\u6267\u884c\u7f16\u8bd1\u540e\u7684MATLAB\u5e94\u7528\u7a0b\u5e8f\u548c\u7ec4\u4ef6\u3002\u5728Java\u4e2d\u8c03\u7528MATLAB\uff0c\u9996\u5148\u9700\u8981\u4f7f\u7528MATLAB Compiler SDK\u5c06MATLAB\u51fd\u6570\u7f16\u8bd1\u4e3aJava\u7c7b\uff0c\u5176\u6b21\u5b89\u88c5MATLAB Runtime\uff0c\u5e76\u5728Java\u9879\u76ee\u4e2d\u5f15\u5165\u6240\u9700\u7684JAR\u6587\u4ef6\uff0c\u6700\u540e\u7f16\u5199Java\u4ee3\u7801\u8c03\u7528\u8fd9\u4e9b\u7c7b\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u8bbe\u7f6eMATLAB\u7f16\u8bd1\u73af\u5883<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u8981\u786e\u4fdd\u4f60\u7684\u7cfb\u7edf\u4e2d\u5b89\u88c5\u6709MATLAB\u548cMATLAB Compiler SDK\u3002\u63a5\u7740\uff0c\u6253\u5f00MATLAB\u5e76\u4e14\u914d\u7f6eMATLAB Compiler SDK\uff0c\u786e\u4fdd\u4f60\u6709\u6743\u9650\u8fdb\u884c\u4ee3\u7801\u7684\u7f16\u8bd1\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\u5728MATLAB\u547d\u4ee4\u7a97\u53e3\u4e2d\u8fd0\u884c<code>mcc -setup java<\/code>\uff0c\u9009\u62e9\u5408\u9002\u7684\u7f16\u8bd1\u5668\u8fdb\u884c\u914d\u7f6e\u3002\u63a5\u4e0b\u6765\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>deploytool<\/code>\u6216\u8005MATLAB\u7f16\u7a0b\u63a5\u53e3\u8fdb\u884c\u7f16\u8bd1\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u7f16\u8bd1MATLAB\u4ee3\u7801\u4e3aJava\u7c7b<\/h3>\n<\/p>\n<p><p>\u4f60\u9700\u8981\u786e\u5b9a\u54ea\u4e9bMATLAB\u51fd\u6570\u5c06\u88abJava\u8c03\u7528\uff0c\u5e76\u5c06\u5b83\u4eec\u4f5c\u4e3a\u5165\u53e3\u70b9\u7f16\u8bd1\u3002\u8fd9\u6837\uff0c\u6bcf\u4e2a\u5165\u53e3\u70b9MATLAB\u51fd\u6570\u90fd\u5c06\u88ab\u7f16\u8bd1\u6210\u4e00\u4e2aJava\u7c7b\u3002<\/p>\n<\/p>\n<p><p>\u8fd9\u53ef\u4ee5\u901a\u8fc7<code>deploytool<\/code>\u56fe\u5f62\u754c\u9762\u6216\u8005\u547d\u4ee4\u884c\u6765\u5b8c\u6210\u3002\u4f8b\u5982\uff0c\u82e5\u6709\u4e00\u4e2aMATLAB\u51fd\u6570<code>myFunction.m<\/code>\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u7f16\u8bd1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-matlab\">mcc -W &#039;java:myPackage,myClass&#039; -T &#039;link:lib&#039; -d output_directory myFunction.m<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8be5\u547d\u4ee4\u4f1a\u5728<code>output_directory<\/code>\u4e0b\u751f\u6210Java\u7c7b<code>myClass<\/code>\u4ee5\u53ca\u5fc5\u8981\u7684JAR\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5b89\u88c5MATLAB Runtime<\/h3>\n<\/p>\n<p><p>\u82e5\u8981\u5728\u6ca1\u6709\u5b89\u88c5MATLAB\u7684\u673a\u5668\u4e0a\u8fd0\u884c\u7f16\u8bd1\u540e\u7684\u4ee3\u7801\uff0c\u9700\u8981\u5b89\u88c5MATLAB Runtime\u3002MATLAB Runtime\u4e0eMATLAB\u7248\u672c\u76f8\u5173\u8054\uff0c\u56e0\u6b64\u9700\u8981\u4e0b\u8f7d\u4e0eMATLAB Compiler SDK\u76f8\u5339\u914d\u7684MATLAB Runtime\u7248\u672c\u3002<\/p>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u8bbe\u7f6e\u7cfb\u7edf\u7684\u73af\u5883\u53d8\u91cf\uff0c\u4ee5\u4fbfJava\u7a0b\u5e8f\u80fd\u627e\u5230MATLAB Runtime\u76f8\u5173\u7684\u5e93\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5728Java\u9879\u76ee\u4e2d\u5f15\u5165JAR\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728\u7f16\u8bd1\u597dMATLAB\u51fd\u6570\u4e4b\u540e\uff0c\u5c06\u751f\u6210\u7684JAR\u6587\u4ef6\u6dfb\u52a0\u5230Java\u9879\u76ee\u4e2d\u3002\u8fd9\u901a\u5e38\u5305\u62ec\u8f93\u51fa\u76ee\u5f55\u4e0b\u7684<code>myclass.jar<\/code>\u4ee5\u53ca\u5176\u4ed6\u4f9d\u8d56\u7684JAR\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u7f16\u5199Java\u4ee3\u7801\u8c03\u7528MATLAB\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5728Java\u4e2d\uff0c\u4f60\u53ef\u4ee5\u5229\u7528MATLAB Compiler SDK\u751f\u6210\u7684\u7c7b\u5982\u540c\u4f7f\u7528\u666e\u901aJava\u7c7b\u4e00\u6837\u8c03\u7528MATLAB\u7b97\u6cd5\u3002\u9996\u5148\uff0c\u521d\u59cb\u5316MATLAB Runtime\u73af\u5883\uff0c\u4e4b\u540e\u521b\u5efa\u5b9e\u4f8b\u5e76\u8c03\u7528\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">import com.mathworks.toolbox.javabuilder.*;<\/p>\n<p>import myPackage.myClass;<\/p>\n<p>public class MatlabInvocation {<\/p>\n<p>    public static void m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n(String[] args) {<\/p>\n<p>        \/\/ \u521d\u59cb\u5316MATLAB Runtime\u7ec4\u4ef6<\/p>\n<p>        MWComponentOptions MWComponentOptions = new MWArrayComponent.Options();<\/p>\n<p>        try {<\/p>\n<p>            \/\/ \u521b\u5efa\u51fd\u6570\u7c7b\u7684\u5b9e\u4f8b<\/p>\n<p>            myClass myFunction = new myClass();<\/p>\n<p>            \/\/ \u8c03\u7528\u51fd\u6570\uff0c\u4f20\u9012\u53c2\u6570\u5e76\u63a5\u6536\u8fd4\u56de\u503c<\/p>\n<p>            Object[] result = myFunction.myFunctionName(1, parameter1, parameter2, ...);<\/p>\n<p>            \/\/ \u5904\u7406\u8fd4\u56de\u503c<\/p>\n<p>            MWArray myResult = (MWArray)result[0];<\/p>\n<p>            \/\/ \u6e05\u7406\u8d44\u6e90<\/p>\n<p>            myFunction.dispose();<\/p>\n<p>        } catch (MWException e) {<\/p>\n<p>            e.printStackTrace();<\/p>\n<p>        } finally {<\/p>\n<p>            MWArray.disposeArray();<\/p>\n<p>        }<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5904\u7406\u6570\u636e\u7c7b\u578b\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u5728Java\u548cMATLAB\u4e4b\u95f4\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u6570\u636e\u7c7b\u578b\u7684\u8f6c\u6362\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u3002MATLAB Compiler SDK\u63d0\u4f9b\u4e86<code>MWArray<\/code>\u7c7b\u65cf\u6765\u5bf9\u5e94\u4e0d\u540c\u7684MATLAB\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><p>\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u82e5MATLAB\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u77e9\u9635\uff0c\u53ef\u4ee5\u5728Java\u4e2d\u4f7f\u7528<code>MWNumericArray<\/code>\u6765\u63a5\u6536\u5e76\u64cd\u4f5c\u5b83\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">MWNumericArray numericArray = (MWNumericArray)result[0];<\/p>\n<p>double[][] arrayData = numericArray.getDoubleData();<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5f02\u5e38\u5904\u7406\u548c<a href=\"https:\/\/docs.pingcode.com\/blog\/project-management\/58557.html\" target=\"_blank\">\u8d44\u6e90\u7ba1\u7406<\/a><\/h3>\n<\/p>\n<p><p>\u5728Java\u5e94\u7528\u7a0b\u5e8f\u4e2d\u8c03\u7528MATLAB\u51fd\u6570\u65f6\uff0c\u5fc5\u987b\u59a5\u5584\u5904\u7406\u5f02\u5e38\uff0c\u5e76\u4e14\u7533\u8bf7\u7684\u4efb\u4f55\u8d44\u6e90\u90fd\u5e94\u8be5\u88ab\u9002\u65f6\u91ca\u653e\u3002\u8fd9\u5305\u62ec\u4f46\u4e0d\u9650\u4e8eMATLAB\u51fd\u6570\u8c03\u7528\u8fd4\u56de\u7684<code>MWArray<\/code>\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">try {<\/p>\n<p>    \/\/ \u521b\u5efa\u51fd\u6570\u7c7b\u7684\u5b9e\u4f8b\u5e76\u8c03\u7528\u51fd\u6570<\/p>\n<p>} catch (MWException e) {<\/p>\n<p>    \/\/ \u5f02\u5e38\u5904\u7406<\/p>\n<p>} finally {<\/p>\n<p>    \/\/ \u6e05\u7406\u64cd\u4f5c\uff0c\u91ca\u653e\u8d44\u6e90<\/p>\n<p>    MWArray.disposeArray();<\/p>\n<p>    if (myFunction != null) {<\/p>\n<p>        myFunction.dispose();<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u5c06MATLAB\u7684\u5f3a\u5927\u8ba1\u7b97\u80fd\u529b\u548c\u56fe\u5f62\u5904\u7406\u529f\u80fd\u96c6\u6210\u5230Java\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u5145\u5206\u5229\u7528MATLAB\u5728\u7b97\u6cd5\u5f00\u53d1\u4e0a\u7684\u4fbf\u6377\u6027\u548cJava\u5728\u5e94\u7528\u90e8\u7f72\u4e0a\u7684\u5e7f\u6cdb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5728Java\u4e2d\u5982\u4f55\u8c03\u7528Matlab\uff1f<\/strong><\/p>\n<p>\u5728Java\u4e2d\u8c03\u7528Matlab\u53ef\u4ee5\u4f7f\u7528Matlab Engine API\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Matlab\u5e76\u4e14\u914d\u7f6e\u4e86Matlab Engine\u3002\u63a5\u4e0b\u6765\uff0c\u5728Java\u9879\u76ee\u4e2d\u5bfc\u5165matlab.engine\u5e93\uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2aMatlab\u5f15\u64ce\u5bf9\u8c61\u3002\u901a\u8fc7\u8be5\u5bf9\u8c61\uff0c\u4f60\u53ef\u4ee5\u8c03\u7528\u5404\u79cdMatlab\u51fd\u6570\uff0c\u6267\u884cMatlab\u547d\u4ee4\uff0c\u5e76\u83b7\u53d6\u7ed3\u679c\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u5728Java\u4e2d\u4f20\u9012\u53c2\u6570\u7ed9Matlab\u51fd\u6570\uff1f<\/strong><\/p>\n<p>\u5728Java\u4e2d\u4f20\u9012\u53c2\u6570\u7ed9Matlab\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7Engine\u5bf9\u8c61\u7684feval\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002feval\u65b9\u6cd5\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff1aMatlab\u51fd\u6570\u540d\u548c\u53c2\u6570\u5217\u8868\u3002\u4f60\u53ef\u4ee5\u6839\u636e\u51fd\u6570\u7684\u8981\u6c42\u4f20\u9012\u76f8\u5e94\u7684\u53c2\u6570\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u5982\u679cMatlab\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u6574\u6570\u53c2\u6570\u548c\u4e00\u4e2a\u5b57\u7b26\u4e32\u53c2\u6570\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code>engine.feval(&quot;myMatlabFunction&quot;, 10, &quot;Hello&quot;);\n<\/code><\/pre>\n<p><strong>3. \u5728Java\u4e2d\u5982\u4f55\u5904\u7406Matlab\u8fd4\u56de\u7684\u7ed3\u679c\uff1f<\/strong><\/p>\n<p>\u5728Java\u4e2d\u5904\u7406Matlab\u8fd4\u56de\u7684\u7ed3\u679c\u53ef\u4ee5\u901a\u8fc7Engine\u5bf9\u8c61\u7684eval\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002eval\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2aMatlab\u547d\u4ee4\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u76f8\u5e94\u7684\u7ed3\u679c\u3002\u4f60\u53ef\u4ee5\u6839\u636e\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002\u4f8b\u5982\uff0c\u5982\u679c\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u77e9\u9635\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u83b7\u53d6\u77e9\u9635\u7684\u884c\u5217\u6570\uff0c\u5e76\u904d\u5386\u8f93\u51fa\u6bcf\u4e2a\u5143\u7d20\uff1a<\/p>\n<pre><code>double[][] result = engine.eval(&quot;myMatlabCommand&quot;);\nint rows = result.length;\nint columns = result[0].length;\nfor (int i = 0; i &lt; rows; i++) {\n    for (int j = 0; j &lt; columns; j++) {\n        System.out.println(result[i][j]);\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u5728Java\u4e2d\u8c03\u7528Matlab\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u4e0d\u540c\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\uff1a\u4f7f\u7528MATLAB Compiler SDK\u63d0\u4f9b\u7684AP [&hellip;]","protected":false},"author":3,"featured_media":295610,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/295591"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=295591"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/295591\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/295610"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=295591"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=295591"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=295591"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}