{"id":173964,"date":"2024-05-08T18:22:24","date_gmt":"2024-05-08T10:22:24","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/173964.html"},"modified":"2024-05-08T18:22:30","modified_gmt":"2024-05-08T10:22:30","slug":"python%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8matlab%e7%9a%84%e8%84%9a%e6%9c%ac","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/173964.html","title":{"rendered":"python\u5982\u4f55\u8c03\u7528matlab\u7684\u811a\u672c"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27050106\/457f1192-d458-4e5b-a14f-577fa3396f52.webp\" alt=\"python\u5982\u4f55\u8c03\u7528matlab\u7684\u811a\u672c\" \/><\/p>\n<p><p><strong>Python\u8c03\u7528MATLAB\u811a\u672c\u53ef\u901a\u8fc7MATLAB Engine API for Python\u5b9e\u73b0\u3001\u5229\u7528MATLAB Compiler SDK\u751f\u6210\u53ef\u71b5\u7acb\u6267\u884c\u7684\u5e94\u7528\u7a0b\u5e8f\u6216\u5e93\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982<code>matlab_wrapper<\/code>\u6216<code>pymatbridge<\/code>\u8fdb\u884c\u6865\u63a5\u3002<\/strong> \u4f7f\u7528MATLAB Engine API for Python\u662f\u6700\u76f4\u63a5\u7684\u65b9\u5f0f\u3002\u6b64API\u5141\u8bb8Python\u76f4\u63a5\u8c03\u7528MATLAB\u51fd\u6570\u3001\u811a\u672c\u548c\u5de5\u4f5c\u533a\u3002\u7528\u6237\u9700\u5148\u5728\u5177\u6709MATLAB\u5b89\u88c5\u7684\u8ba1\u7b97\u673a\u4e0a\u5b89\u88c5\u6b64API\uff0c\u7136\u540e\u5373\u53ef\u5728Python\u811a\u672c\u4e2d\u542f\u52a8MATLAB\u4f1a\u8bdd\uff0c\u5e76\u8fd0\u884cMATLAB\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u5982\u4e0b\u65b9\u5f0f\u4f7f\u7528MATLAB Engine API for Python\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5728\u547d\u4ee4\u63d0\u793a\u7b26\u4e0b\uff0c\u8f93\u5165<code>python<\/code>\u542f\u52a8Python\u73af\u5883\u3002<\/li>\n<li>\u5bfc\u5165MATLAB\u5f15\u64ce\u6a21\u5757\uff1a<code>import matlab.engine<\/code>\u3002<\/li>\n<li>\u542f\u52a8MATLAB\u5f15\u64ce\u4f1a\u8bdd\uff1a<code>eng = matlab.engine.start_matlab()<\/code>\u3002<\/li>\n<li>\u8fd0\u884cMATLAB\u811a\u672c\uff1a<code>eng.eval(&quot;script_name&quot;, nargout=0)<\/code>\uff0c\u5176\u4e2d<code>script_name<\/code>\u662f\u60a8\u7684MATLAB\u811a\u672c\u540d\u79f0\u3002<\/li>\n<\/ol>\n<p><p>\u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u6df1\u5165\u63a2\u8ba8\u5982\u4f55\u4ecePython\u8c03\u7528MATLAB\u811a\u672c\u7684\u4e0d\u540c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528MATLAB Engine API for Python<\/h3>\n<\/p>\n<p><p>\u6b64\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5728Python\u4e2d\u7d27\u5bc6\u96c6\u6210MATLAB\u4ee3\u7801\u7684\u60c5\u51b5\u3002\u4ee5\u4e0b\u662f\u9010\u6b65\u8bf4\u660e\uff1a<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5MATLAB Engine for Python<\/h4>\n<\/p>\n<ol>\n<li>\u6253\u5f00MATLAB\u5e76\u5b9a\u4f4d\u5230engine\u5b89\u88c5\u76ee\u5f55\uff0c\u901a\u5e38\u5728<code>matlabroot\/extern\/engines\/python<\/code>\u3002<\/li>\n<li>\u5728MATLAB\u547d\u4ee4\u7a97\u53e3\u4e2d\uff0c\u8fd0\u884c<code>cd(matlabroot + &#039;\/extern\/engines\/python&#039;)<\/code>\u3002<\/li>\n<li>\u7136\u540e\u5728\u547d\u4ee4\u63d0\u793a\u7b26\u4e0b\u6267\u884c\u5b89\u88c5\u547d\u4ee4<code>python setup.py install<\/code>\u3002<\/li>\n<\/ol>\n<p><h4>\u542f\u52a8\u548c\u4f7f\u7528MATLAB Engine<\/h4>\n<\/p>\n<p><p>\u5728\u5b89\u88c5\u597dMATLAB Engine for Python\u540e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matlab.engine<\/p>\n<h2><strong>\u542f\u52a8MATLAB\u5f15\u64ce<\/strong><\/h2>\n<p>eng = matlab.engine.start_matlab()<\/p>\n<h2><strong>\u8c03\u7528MATLAB\u811a\u672c<\/strong><\/h2>\n<p>eng.script_name(nargout=0)<\/p>\n<h2><strong>\u5173\u95edMATLAB\u5f15\u64ce<\/strong><\/h2>\n<p>eng.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>nargout=0<\/code>\u8868\u793a\u4e0d\u671f\u671b\u4ece\u811a\u672c\u4e2d\u8fd4\u56de\u8f93\u51fa\u53c2\u6570\u3002\u5982\u679cMATLAB\u811a\u672c\u6709\u8f93\u51fa\uff0c\u9700\u8981\u66f4\u6539<code>nargout<\/code>\u53c2\u6570\u7684\u503c\uff0c\u5e76\u5c06\u51fd\u6570\u8f93\u51fa\u8d4b\u503c\u7ed9Python\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001MATLAB Compiler SDK<\/h3>\n<\/p>\n<p><p>MATLAB Compiler SDK\u5141\u8bb8\u5c06MATLAB\u51fd\u6570\u8f6c\u6362\u4e3a\u5e93\uff0c\u8fd9\u4e9b\u5e93\u53ef\u4ee5\u5728\u6ca1\u6709MATLAB\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\u5728Python\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>\u7f16\u8bd1MATLAB\u4ee3\u7801<\/h4>\n<\/p>\n<ol>\n<li>\u4f7f\u7528MATLAB Compiler SDK\u5728MATLAB\u4e2d\u7f16\u8bd1\u4ee3\u7801\uff1a<code>mcc -W cpplib:libname -T link:lib script_name.m<\/code>\u3002<\/li>\n<li>\u8fd9\u4f1a\u751f\u6210\u4e00\u4e2a\u5e93\u6587\u4ef6\u53ca\u5176\u9644\u5e26\u7684\u5934\u6587\u4ef6\u548c\u5e93\u6587\u4ef6\u3002<\/li>\n<\/ol>\n<p><h4>\u8c03\u7528\u7f16\u8bd1\u540e\u7684\u5e93<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ctypes<\/p>\n<h2><strong>\u52a0\u8f7d\u7f16\u8bd1\u597d\u7684MATLAB\u5e93<\/strong><\/h2>\n<p>libname = ctypes.CDLL(&#039;\/path\/to\/libname.so&#039;)<\/p>\n<h2><strong>\u521d\u59cb\u5316MATLAB Runtime<\/strong><\/h2>\n<p>if libname.mclInitializeApplication(None,0):<\/p>\n<p>    # \u8c03\u7528\u5e93\u51fd\u6570<\/p>\n<p>    libname.mtFunctionName()<\/p>\n<p>    # \u7ec8\u6b62MATLAB Runtime<\/p>\n<p>    libname.mclTerminateApplication()<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Could not initialize the MATLAB Runtime.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/h3>\n<\/p>\n<p><p>\u7b2c\u4e09\u65b9\u5e93\u5982<code>matlab_wrapper<\/code>\u6216<code>pymatbridge<\/code>\u53ef\u4ee5\u4f5c\u4e3a\u6865\u63a5Python\u548cMATLAB\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5\u7b2c\u4e09\u65b9\u5e93<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install matlab_wrapper<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5728Python\u4e2d\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from matlab_wrapper import MatlabSession<\/p>\n<h2><strong>\u521b\u5efaMATLAB\u4f1a\u8bdd<\/strong><\/h2>\n<p>with MatlabSession() as session:<\/p>\n<p>    # \u8fd0\u884cMATLAB\u811a\u672c<\/p>\n<p>    session.run(&#039;script_name.m&#039;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u7279\u5b9a\u7684\u9002\u7528\u573a\u666f\u548c\u9650\u5236\u3002<strong>\u5728\u9009\u62e9\u5982\u4f55\u8c03\u7528MATLAB\u811a\u672c\u65f6\uff0c\u5e94\u8003\u8651\u9879\u76ee\u7684\u8981\u6c42\u3001MATLAB\u4e0ePython\u95f4\u7684\u4ea4\u4e92\u7a0b\u5ea6\u4ee5\u53ca\u662f\u5426\u9700\u8981MATLAB\u8fd0\u884c\u65f6\u73af\u5883\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u8c03\u7528MATLAB\u811a\u672c\u662f\u8de8\u8bed\u8a00\u7f16\u7a0b\u7684\u4e00\u4e2a\u91cd\u8981\u5b9e\u8df5\uff0c\u5b83\u5141\u8bb8\u5f00\u53d1\u8005\u5229\u7528MATLAB\u5f3a\u5927\u7684\u5de5\u7a0b\u548c\u79d1\u5b66\u8ba1\u7b97\u529f\u80fd\u6765\u589e\u5f3aPython\u9879\u76ee\u7684\u80fd\u529b\u3002\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0cPython\u5f00\u53d1\u8005\u53ef\u4ee5\u6709\u6548\u5730\u96c6\u6210\u548c\u5229\u7528MATLAB\u811a\u672c\uff0c\u4e3a\u79d1\u5b66\u7814\u7a76\u548c\u5de5\u7a0b\u5e94\u7528\u63d0\u4f9b\u5f3a\u5927\u7684\u652f\u6301\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u4f7f\u7528MATLAB\u5f15\u64ce\u7684Python\u5305\u5982\u4f55\u8c03\u7528MATLAB\u811a\u672c\uff1f<\/strong><\/p>\n<p>\u8981\u5728Python\u4e2d\u8c03\u7528MATLAB\u811a\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528MATLAB\u5f15\u64ce\u7684Python\u5305\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86MATLAB\u5f15\u64ce\uff0c\u5e76\u5c06\u5176\u914d\u7f6e\u4e3a\u53ef\u4f9bPython\u8bbf\u95ee\u3002\u7136\u540e\uff0c\u901a\u8fc7import matlab.engine\u8bed\u53e5\u5bfc\u5165matlab.engine\u5305\u3002\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528matlab.engine.start_matlab()\u65b9\u6cd5\u542f\u52a8MATLAB\u5f15\u64ce\u3002\u4e00\u65e6\u5f15\u64ce\u542f\u52a8\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528eng.eval()\u65b9\u6cd5\u6765\u8c03\u7528MATLAB\u811a\u672c\u3002\u5c06\u811a\u672c\u7684\u8def\u5f84\u548c\u540d\u79f0\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9eval()\u65b9\u6cd5\uff0c\u5e76\u6839\u636e\u9700\u8981\u4f20\u9012\u4efb\u4f55\u5fc5\u8981\u7684\u8f93\u5165\u53c2\u6570\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u5728Python\u4e2d\u8c03\u7528MATLAB\u811a\u672c\u5e76\u4ee5\u9002\u5f53\u7684\u65b9\u5f0f\u5904\u7406\u8f93\u51fa\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u5728Python\u4e2d\u5c06\u6570\u636e\u4f20\u9012\u7ed9MATLAB\u811a\u672c\u5e76\u5904\u7406\u8fd4\u56de\u7684\u7ed3\u679c\uff1f<\/strong><\/p>\n<p>\u8981\u5c06\u6570\u636e\u4f20\u9012\u7ed9MATLAB\u811a\u672c\u5e76\u5904\u7406\u8fd4\u56de\u7684\u7ed3\u679c\uff0c\u9700\u8981\u5148\u5c06\u6570\u636e\u8f6c\u6362\u4e3aMATLAB\u5f15\u64ce\u53ef\u8bc6\u522b\u7684\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528numpy\u6570\u7ec4\u5c06Python\u4e2d\u7684\u6570\u636e\u8f6c\u6362\u4e3aMATLAB\u4e2d\u7684\u77e9\u9635\u3002\u7136\u540e\uff0c\u901a\u8fc7matlab.double()\u65b9\u6cd5\u5c06\u6570\u636e\u4f20\u9012\u7ed9eval()\u65b9\u6cd5\u3002\u5728MATLAB\u811a\u672c\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u76f8\u540c\u7684\u65b9\u5f0f\u5c06\u7ed3\u679c\u8fd4\u56de\u7ed9Python\u3002\u5c06\u7ed3\u679c\u8f6c\u6362\u4e3a\u9002\u5f53\u7684Python\u6570\u636e\u7c7b\u578b\uff0c\u5e76\u8fdb\u884c\u540e\u7eed\u5904\u7406\u3002\u8fd9\u6837\uff0c\u60a8\u53ef\u4ee5\u5728Python\u4e2d\u4f20\u9012\u6570\u636e\u7ed9MATLAB\u811a\u672c\uff0c\u5e76\u5904\u7406\u8fd4\u56de\u7684\u7ed3\u679c\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u9519\u8bef\u6216\u5f02\u5e38\u60c5\u51b5\uff1f<\/strong><\/p>\n<p>\u5728\u8c03\u7528MATLAB\u811a\u672c\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u9519\u8bef\u6216\u5f02\u5e38\u60c5\u51b5\u3002\u4e3a\u4e86\u5904\u7406\u8fd9\u4e9b\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528try-except\u8bed\u53e5\u5757\u5728Python\u4e2d\u6355\u83b7\u53ef\u80fd\u629b\u51fa\u7684\u5f02\u5e38\u3002\u540c\u65f6\uff0c\u5efa\u8bae\u5728\u8c03\u7528MATLAB\u811a\u672c\u4e4b\u524d\u8fdb\u884c\u4e00\u4e9b\u9884\u5904\u7406\u5de5\u4f5c\uff0c\u5982\u68c0\u67e5\u8f93\u5165\u53c2\u6570\u7684\u6709\u6548\u6027\uff0c\u907f\u514d\u65e0\u6548\u7684\u8c03\u7528\u6216\u6267\u884c\u3002\u5982\u679c\u5728MATLAB\u811a\u672c\u4e2d\u51fa\u73b0\u9519\u8bef\uff0c\u7a0b\u5e8f\u4f1a\u629b\u51faMATLAB\u5f02\u5e38\u3002\u53ef\u4ee5\u901a\u8fc7\u6355\u83b7\u8be5\u5f02\u5e38\u5e76\u8fdb\u884c\u9002\u5f53\u7684\u5904\u7406\uff0c\u4f8b\u5982\u6253\u5370\u9519\u8bef\u6d88\u606f\u6216\u91c7\u53d6\u5176\u4ed6\u7ea0\u6b63\u63aa\u65bd\u3002\u8fd9\u6837\uff0c\u60a8\u53ef\u4ee5\u6709\u6548\u5730\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u9519\u8bef\u6216\u5f02\u5e38\u60c5\u51b5\uff0c\u63d0\u9ad8\u811a\u672c\u8c03\u7528\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u9760\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8c03\u7528MATLAB\u811a\u672c\u53ef\u901a\u8fc7MATLAB Engine API for Python\u5b9e\u73b0\u3001\u5229\u7528MAT [&hellip;]","protected":false},"author":3,"featured_media":173973,"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\/173964"}],"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=173964"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/173964\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/173973"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=173964"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=173964"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=173964"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}