{"id":968735,"date":"2024-12-27T05:09:45","date_gmt":"2024-12-26T21:09:45","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/968735.html"},"modified":"2024-12-27T05:09:46","modified_gmt":"2024-12-26T21:09:46","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8python%e4%bd%bf%e7%94%a8matlab","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/968735.html","title":{"rendered":"\u5982\u4f55\u5728python\u4f7f\u7528matlab"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24183348\/71824053-55b6-4dbd-a1b1-01dc5da7d876.webp\" alt=\"\u5982\u4f55\u5728python\u4f7f\u7528matlab\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u4f7f\u7528MATLAB\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c<strong>\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528MATLAB Engine API\u3001\u901a\u8fc7MATLAB Runtime\u6267\u884c\u7f16\u8bd1\u7684MATLAB\u4ee3\u7801\u3001\u5229\u7528MATLAB\u7684Python\u63a5\u53e3\u5305<\/strong>\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7MATLAB Engine API for Python\uff0c\u5b83\u5141\u8bb8\u5728Python\u73af\u5883\u4e2d\u8c03\u7528MATLAB\u529f\u80fd\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528MATLAB Engine API\u6765\u5b9e\u73b0Python\u4e0eMATLAB\u7684\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<p><p>MATLAB Engine API\u662f\u4e00\u5957\u63d0\u4f9bPython\u4e0eMATLAB\u4e4b\u95f4\u901a\u4fe1\u7684\u5de5\u5177\u96c6\u3002\u5229\u7528\u5b83\uff0cPython\u7528\u6237\u53ef\u4ee5\u542f\u52a8MATLAB\u8fdb\u7a0b\u3001\u8c03\u7528MATLAB\u51fd\u6570\u3001\u4f20\u9012\u6570\u636e\uff0c\u5e76\u83b7\u53d6MATLAB\u8ba1\u7b97\u7ed3\u679c\u3002\u4f7f\u7528MATLAB Engine API\u7684\u4e00\u4e2a\u5173\u952e\u4f18\u52bf\u662f\u7528\u6237\u53ef\u4ee5\u5145\u5206\u5229\u7528MATLAB\u5f3a\u5927\u7684\u6570\u5b66\u8ba1\u7b97\u80fd\u529b\u548c\u4e30\u5bcc\u7684\u5de5\u5177\u7bb1\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5MATLAB Engine API<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528MATLAB Engine API\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u7cfb\u7edf\u4e2d\u5df2\u5b89\u88c5MATLAB\u8f6f\u4ef6\uff0c\u5e76\u914d\u7f6e\u597dPython\u73af\u5883\u3002MATLAB Engine API\u5e76\u975e\u9ed8\u8ba4\u5b89\u88c5\u7684\u7ec4\u4ef6\uff0c\u9700\u8981\u624b\u52a8\u5b89\u88c5\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u914d\u7f6ePython\u73af\u5883<\/strong><\/p>\n<\/p>\n<p><p>\u786e\u4fddPython\u5df2\u5b89\u88c5\u4e14\u8def\u5f84\u5df2\u6dfb\u52a0\u5230\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u4e2d\u3002\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u8f93\u5165<code>python --version<\/code>\u6765\u68c0\u67e5Python\u662f\u5426\u5b89\u88c5\u6210\u529f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b89\u88c5MATLAB Engine<\/strong><\/p>\n<\/p>\n<ul>\n<li>\n<p>\u6253\u5f00MATLAB\uff0c\u5e76\u5bfc\u822a\u81f3MATLAB\u6839\u76ee\u5f55\u4e0b\u7684<code>extern\/engines\/python<\/code>\u6587\u4ef6\u5939\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u5728\u547d\u4ee4\u884c\u4e2d\u8fdb\u5165\u8be5\u76ee\u5f55\uff0c\u5e76\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code>python setup.py install<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><p>\u8fd9\u5c06\u5b89\u88c5MATLAB Engine API for Python\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u4f7f\u7528MATLAB Engine API<\/h3>\n<\/p>\n<p><h4>\u542f\u52a8MATLAB\u5f15\u64ce<\/h4>\n<\/p>\n<p><p>\u542f\u52a8MATLAB\u5f15\u64ce\u662f\u4f7f\u7528MATLAB Engine API\u7684\u7b2c\u4e00\u6b65\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u542f\u52a8MATLAB\u5f15\u64ce\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matlab.engine<\/p>\n<h2><strong>\u542f\u52a8\u5f15\u64ce<\/strong><\/h2>\n<p>eng = matlab.engine.start_matlab()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4ee3\u7801\u7247\u6bb5\u5c06\u542f\u52a8\u4e00\u4e2aMATLAB\u8fdb\u7a0b\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5f15\u64ce\u5bf9\u8c61<code>eng<\/code>\uff0c\u8be5\u5bf9\u8c61\u7528\u4e8e\u540e\u7eed\u7684MATLAB\u547d\u4ee4\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><h4>\u8c03\u7528MATLAB\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u4e00\u65e6\u5f15\u64ce\u542f\u52a8\uff0c\u5c31\u53ef\u4ee5\u8c03\u7528MATLAB\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u8ba1\u7b97\u4e24\u4e2a\u6570\u7684\u548c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8c03\u7528MATLAB\u4e2d\u7684sum\u51fd\u6570<\/p>\n<p>result = eng.sum([1.0, 2.0, 3.0])<\/p>\n<p>print(&quot;The sum is:&quot;, result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cMATLAB\u7684<code>sum<\/code>\u51fd\u6570\u88ab\u8c03\u7528\u4ee5\u8ba1\u7b97\u6570\u7ec4\u5143\u7d20\u7684\u548c\u3002<\/p>\n<\/p>\n<p><h4>\u4f20\u9012\u6570\u636e<\/h4>\n<\/p>\n<p><p>MATLAB Engine API\u652f\u6301\u5728Python\u548cMATLAB\u4e4b\u95f4\u4f20\u9012\u6570\u636e\u3002\u4f8b\u5982\uff0c\u4f20\u9012\u4e00\u4e2a\u77e9\u9635\u8fdb\u884c\u8ba1\u7b97\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2anumpy\u6570\u7ec4<\/strong><\/h2>\n<p>matrix = np.array([[1, 2], [3, 4]])<\/p>\n<h2><strong>\u5c06numpy\u6570\u7ec4\u8f6c\u6362\u4e3aMATLAB\u6570\u7ec4<\/strong><\/h2>\n<p>matlab_matrix = matlab.double(matrix.tolist())<\/p>\n<h2><strong>\u8c03\u7528MATLAB\u51fd\u6570\u8fdb\u884c\u8ba1\u7b97\uff08\u5982\u77e9\u9635\u6c42\u9006\uff09<\/strong><\/h2>\n<p>inverse_matrix = eng.inv(matlab_matrix)<\/p>\n<p>print(&quot;Inverse matrix:&quot;, inverse_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0cPython\u4e2d\u7684NumPy\u6570\u7ec4\u88ab\u8f6c\u6362\u4e3aMATLAB\u517c\u5bb9\u7684\u6570\u7ec4\u683c\u5f0f\uff0c\u5e76\u4f20\u9012\u7ed9MATLAB\u8fdb\u884c\u6c42\u9006\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001MATLAB\u7f16\u8bd1\u4e0eMATLAB Runtime<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528MATLAB Engine API\uff0c\u53e6\u4e00\u79cd\u5728Python\u4e2d\u4f7f\u7528MATLAB\u7684\u65b9\u6cd5\u662f\u5c06MATLAB\u4ee3\u7801\u7f16\u8bd1\u6210\u53ef\u6267\u884c\u7684\u5e93\u6216\u5e94\u7528\u7a0b\u5e8f\uff0c\u5e76\u901a\u8fc7MATLAB Runtime\u8fd0\u884c\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5206\u53d1MATLAB\u529f\u80fd\u800c\u4e0d\u8981\u6c42\u7528\u6237\u5b89\u88c5\u5b8c\u6574MATLAB\u8f6f\u4ef6\u7684\u60c5\u666f\u3002<\/p>\n<\/p>\n<p><h4>\u7f16\u8bd1MATLAB\u4ee3\u7801<\/h4>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528MATLAB Compiler<\/strong><\/p>\n<\/p>\n<p><p>MATLAB\u63d0\u4f9b\u4e86MATLAB Compiler\u5de5\u5177\u7bb1\uff0c\u53ef\u4ee5\u5c06MATLAB\u4ee3\u7801\u7f16\u8bd1\u4e3aPython\u53ef\u8c03\u7528\u7684\u5e93\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p>\u5728MATLAB\u4e2d\u7f16\u5199\u9700\u8981\u7f16\u8bd1\u7684\u51fd\u6570\u811a\u672c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528MATLAB Compiler\u7684<code>mcc<\/code>\u547d\u4ee4\u8fdb\u884c\u7f16\u8bd1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-matlab\">mcc -m my_function.m<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><p>\u8fd9\u5c06\u751f\u6210\u4e00\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\u6216\u5171\u4eab\u5e93\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u914d\u7f6eMATLAB Runtime<\/strong><\/p>\n<\/p>\n<ul>\n<li>MATLAB Runtime\u662f\u4e00\u4e2a\u514d\u8d39\u7684\u8fd0\u884c\u65f6\u73af\u5883\uff0c\u7528\u4e8e\u6267\u884c\u7f16\u8bd1\u7684MATLAB\u5e94\u7528\u3002\u7528\u6237\u9700\u8981\u5b89\u88c5\u4e0e\u7f16\u8bd1MATLAB\u7248\u672c\u5bf9\u5e94\u7684MATLAB Runtime\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><h4>\u5728Python\u4e2d\u8c03\u7528\u7f16\u8bd1\u7684MATLAB\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u7f16\u8bd1\u540e\u7684MATLAB\u4ee3\u7801\u53ef\u4ee5\u5728Python\u4e2d\u901a\u8fc7<code>ctypes<\/code>\u6216<code>cffi<\/code>\u7b49\u5e93\u8fdb\u884c\u8c03\u7528\uff0c\u5177\u4f53\u5b9e\u73b0\u89c6\u7f16\u8bd1\u7ed3\u679c\u7c7b\u578b\u800c\u5b9a\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5229\u7528MATLAB\u7684Python\u63a5\u53e3\u5305<\/h3>\n<\/p>\n<p><p>MATLAB R2021b\u53ca\u4ee5\u540e\u7684\u7248\u672c\u63d0\u4f9b\u4e86Python\u63a5\u53e3\u5305<code>matlab<\/code>\uff0c\u5b83\u5141\u8bb8\u7528\u6237\u76f4\u63a5\u5728Python\u4e2d\u4f7f\u7528MATLAB\u7684\u51fd\u6570\u548c\u5e93\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528Python\u63a5\u53e3\u5305<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5MATLAB Python\u63a5\u53e3\u5305\u540e\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728Python\u4e2d\u4f7f\u7528MATLAB\u51fd\u6570\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\u5185\u7f6e\u51fd\u6570<\/strong><\/h2>\n<p>output = eng.sqrt(16.0)<\/p>\n<p>print(&quot;Square root:&quot;, output)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u63d0\u4f9b\u4e86\u4e0eMATLAB Engine API\u7c7b\u4f3c\u7684\u529f\u80fd\uff0c\u7b80\u5316\u4e86Python\u4e0eMATLAB\u7684\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4ecb\u7ecd\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c<strong>\u5728Python\u4e2d\u4f7f\u7528MATLAB\u53ef\u4ee5\u901a\u8fc7MATLAB Engine API\u3001\u7f16\u8bd1MATLAB\u4ee3\u7801\u5e76\u4f7f\u7528MATLAB Runtime\u3001\u4ee5\u53ca\u4f7f\u7528MATLAB\u7684Python\u63a5\u53e3\u5305\u6765\u5b9e\u73b0<\/strong>\u3002\u4e0d\u540c\u65b9\u6cd5\u5404\u6709\u4f18\u52a3\uff0c\u7528\u6237\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u5f0f\u3002MATLAB Engine API\u9002\u5408\u9700\u8981\u5b9e\u65f6\u4ea4\u4e92\u548c\u5927\u91cf\u6570\u636e\u4ea4\u6362\u7684\u573a\u666f\uff0c\u800c\u7f16\u8bd1\u548c\u4f7f\u7528MATLAB Runtime\u66f4\u9002\u5408\u5206\u53d1\u548c\u90e8\u7f72MATLAB\u529f\u80fd\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u5f0f\uff0c\u90fd\u53ef\u4ee5\u501f\u52a9MATLAB\u5f3a\u5927\u7684\u8ba1\u7b97\u80fd\u529b\u548c\u4e30\u5bcc\u7684\u5de5\u5177\u7bb1\u6765\u589e\u5f3aPython\u9879\u76ee\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u5982\u4f55\u8c03\u7528MATLAB\u529f\u80fd\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>matlab.engine<\/code>\u6a21\u5757\u5728Python\u4e2d\u8c03\u7528MATLAB\u529f\u80fd\u3002\u9996\u5148\u9700\u8981\u5b89\u88c5MATLAB\u5f15\u64ceAPI\uff0c\u786e\u4fddMATLAB\u5df2\u7ecf\u5b89\u88c5\u5e76\u4e14\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u8bbf\u95ee\u3002\u5b89\u88c5\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7<code>import matlab.engine<\/code>\u5bfc\u5165\u6a21\u5757\uff0c\u542f\u52a8MATLAB\u5f15\u64ce\uff0c\u7136\u540e\u8c03\u7528MATLAB\u51fd\u6570\u5e76\u4f20\u9012\u53c2\u6570\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u8fd0\u884cMATLAB\u811a\u672c\u662f\u5426\u6709\u7279\u522b\u7684\u8981\u6c42\uff1f<\/strong><br \/>\u8fd0\u884cMATLAB\u811a\u672c\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u811a\u672c\u6587\u4ef6\u7684\u8def\u5f84\u5728MATLAB\u7684\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u4e2d\uff0c\u6216\u8005\u4f7f\u7528MATLAB\u7684<code>cd<\/code>\u547d\u4ee4\u66f4\u6539\u76ee\u5f55\u3002\u6b64\u5916\uff0cMATLAB\u811a\u672c\u4e2d\u4f7f\u7528\u7684\u6240\u6709\u51fd\u6570\u548c\u53d8\u91cf\u90fd\u5fc5\u987b\u5728MATLAB\u73af\u5883\u4e2d\u5b9a\u4e49\u5e76\u53ef\u7528\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u4e0eMATLAB\u8fdb\u884c\u6570\u636e\u4ea4\u4e92\u65f6\uff0c\u652f\u6301\u54ea\u4e9b\u6570\u636e\u7c7b\u578b\uff1f<\/strong><br \/>\u5728Python\u548cMATLAB\u4e4b\u95f4\u8fdb\u884c\u6570\u636e\u4ea4\u4e92\u65f6\uff0c\u652f\u6301\u591a\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u6570\u503c\u6570\u7ec4\u3001\u5b57\u7b26\u4e32\u3001\u5217\u8868\u3001\u5b57\u5178\u7b49\u3002\u4f7f\u7528<code>matlab.double()<\/code>\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06Python\u7684\u5217\u8868\u6216NumPy\u6570\u7ec4\u8f6c\u6362\u4e3aMATLAB\u652f\u6301\u7684\u7c7b\u578b\uff0c\u4ee5\u4fbf\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u5904\u7406\u548c\u5206\u6790\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u4f7f\u7528MATLAB\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528MATLAB Engine API\u3001\u901a\u8fc7M [&hellip;]","protected":false},"author":3,"featured_media":968742,"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\/968735"}],"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=968735"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/968735\/revisions"}],"predecessor-version":[{"id":968743,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/968735\/revisions\/968743"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/968742"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=968735"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=968735"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=968735"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}