{"id":1058554,"date":"2024-12-31T15:17:24","date_gmt":"2024-12-31T07:17:24","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1058554.html"},"modified":"2024-12-31T15:17:26","modified_gmt":"2024-12-31T07:17:26","slug":"%e5%a6%82%e4%bd%95%e5%b0%86matlab%e7%9a%84%e5%87%bd%e6%95%b0%e5%af%bc%e5%85%a5python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1058554.html","title":{"rendered":"\u5982\u4f55\u5c06matlab\u7684\u51fd\u6570\u5bfc\u5165python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/f3926b18-4178-484d-82a2-91615b0cd6ba.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u5c06matlab\u7684\u51fd\u6570\u5bfc\u5165python\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u5c06MATLAB\u7684\u51fd\u6570\u5bfc\u5165Python<\/strong><\/p>\n<\/p>\n<p><p><strong>\u5c06MATLAB\u7684\u51fd\u6570\u5bfc\u5165Python\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u4f7f\u7528MATLAB Engine API\u3001\u8f6c\u6362\u4e3aPython\u4ee3\u7801\u3001\u4f7f\u7528MATLAB Compiler SDK\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5de5\u5177\u3002<\/strong> \u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c<strong>\u4f7f\u7528MATLAB Engine API<\/strong> \u662f\u4e00\u79cd\u975e\u5e38\u6709\u6548\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528MATLAB\u7684\u51fd\u6570\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><p>MATLAB Engine API for Python \u662f\u4e00\u4e2a\u5141\u8bb8\u4f60\u5728Python\u4e2d\u8c03\u7528MATLAB\u51fd\u6570\u548c\u811a\u672c\u7684\u5e93\u3002\u8981\u4f7f\u7528\u8fd9\u4e2aAPI\uff0c\u4f60\u9996\u5148\u9700\u8981\u786e\u4fdd\u5b89\u88c5\u4e86MATLAB\u548cPython\uff0c\u540c\u65f6\u8fd8\u9700\u8981\u5b89\u88c5MATLAB Engine API\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u4f60\u5c31\u53ef\u4ee5\u5728Python\u4e2d\u76f4\u63a5\u8c03\u7528MATLAB\u51fd\u6570\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u5177\u4f53\u7684\u6b65\u9aa4\u548c\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5MATLAB Engine API for Python<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5MATLAB Engine API for Python\u3002\u5047\u8bbe\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86MATLAB\uff0c\u5e76\u4e14\u5c06\u5176\u6dfb\u52a0\u5230\u4e86\u7cfb\u7edf\u7684\u73af\u5883\u53d8\u91cf\u4e2d\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u6765\u5b89\u88c5MATLAB Engine API\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00\u547d\u4ee4\u884c\u754c\u9762\uff08Windows\u7684CMD\u6216Linux\/Mac\u7684\u7ec8\u7aef\uff09\u3002<\/li>\n<li>\u5bfc\u822a\u5230MATLAB\u7684\u5b89\u88c5\u76ee\u5f55\uff0c\u4f8b\u5982\uff1a\n<pre><code>cd &quot;C:\\Program Files\\MATLAB\\R2023a\\extern\\engines\\python&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5MATLAB Engine API\uff1a\n<pre><code>python setup.py install<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u5728Python\u4e2d\u4f7f\u7528MATLAB Engine<\/h3>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u4f60\u5c31\u53ef\u4ee5\u5728Python\u4ee3\u7801\u4e2d\u5bfc\u5165\u5e76\u4f7f\u7528MATLAB Engine\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5728Python\u4e2d\u8c03\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\u51fd\u6570<\/strong><\/h2>\n<p>result = eng.sqrt(4.0)<\/p>\n<p>print(result)<\/p>\n<h2><strong>\u505c\u6b62MATLAB\u5f15\u64ce<\/strong><\/h2>\n<p>eng.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u542f\u52a8\u4e86\u4e00\u4e2aMATLAB\u5f15\u64ce\uff0c\u7136\u540e\u8c03\u7528\u4e86MATLAB\u7684 <code>sqrt<\/code> \u51fd\u6570\u6765\u8ba1\u7b97\u5e73\u65b9\u6839\uff0c\u6700\u540e\u505c\u6b62\u4e86MATLAB\u5f15\u64ce\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u8c03\u7528\u81ea\u5b9a\u4e49\u7684MATLAB\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684MATLAB\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u5176\u4fdd\u5b58\u5728\u4e00\u4e2a <code>.m<\/code> \u6587\u4ef6\u4e2d\uff0c\u7136\u540e\u5728Python\u4ee3\u7801\u4e2d\u8c03\u7528\u3002\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u540d\u4e3a <code>myfunc.m<\/code> \u7684MATLAB\u51fd\u6570\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-matlab\">function result = myfunc(a, b)<\/p>\n<p>    result = a + b;<\/p>\n<p>end<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0bPython\u4ee3\u7801\u6765\u8c03\u7528\u8fd9\u4e2a\u81ea\u5b9a\u4e49\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>\u5c06\u5f53\u524d\u76ee\u5f55\u6dfb\u52a0\u5230MATLAB\u8def\u5f84\u4e2d<\/strong><\/h2>\n<p>eng.addpath(r&#39;C:\\path\\to\\your\\mfile&#39;)<\/p>\n<h2><strong>\u8c03\u7528\u81ea\u5b9a\u4e49MATLAB\u51fd\u6570<\/strong><\/h2>\n<p>result = eng.myfunc(3.0, 5.0)<\/p>\n<p>print(result)<\/p>\n<h2><strong>\u505c\u6b62MATLAB\u5f15\u64ce<\/strong><\/h2>\n<p>eng.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001MATLAB Compiler SDK<\/h3>\n<\/p>\n<p><p>MATLAB Compiler SDK \u662f\u53e6\u4e00\u79cd\u5c06MATLAB\u4ee3\u7801\u5bfc\u5165Python\u7684\u65b9\u6cd5\u3002\u901a\u8fc7MATLAB Compiler SDK\uff0c\u4f60\u53ef\u4ee5\u5c06MATLAB\u51fd\u6570\u6253\u5305\u6210\u4e00\u4e2a\u5171\u4eab\u5e93\uff0c\u7136\u540e\u5728Python\u4e2d\u8c03\u7528\u8fd9\u4e2a\u5171\u4eab\u5e93\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528MATLAB Compiler SDK\u7684\u57fa\u672c\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5728MATLAB\u4e2d\u4f7f\u7528 <code>deploytool<\/code> \u547d\u4ee4\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u9879\u76ee\u3002<\/li>\n<li>\u5c06\u4f60\u7684MATLAB\u51fd\u6570\u6dfb\u52a0\u5230\u9879\u76ee\u4e2d\u3002<\/li>\n<li>\u751f\u6210\u5171\u4eab\u5e93\uff08\u4f8b\u5982 <code>.dll<\/code> \u6587\u4ef6\uff09\u3002<\/li>\n<li>\u5728Python\u4e2d\u4f7f\u7528 <code>ctypes<\/code> \u6216 <code>cffi<\/code> \u6a21\u5757\u6765\u52a0\u8f7d\u548c\u8c03\u7528\u5171\u4eab\u5e93\u3002<\/li>\n<\/ol>\n<p><h3>\u4e94\u3001\u7b2c\u4e09\u65b9\u5de5\u5177<\/h3>\n<\/p>\n<p><p>\u9664\u4e86MATLAB\u5b98\u65b9\u63d0\u4f9b\u7684\u65b9\u6cd5\uff0c\u8fd8\u6709\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5c06MATLAB\u51fd\u6570\u5bfc\u5165Python\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>M2Py<\/strong>\uff1a\u4e00\u4e2a\u5c06MATLAB\u4ee3\u7801\u8f6c\u6362\u4e3aPython\u4ee3\u7801\u7684\u5de5\u5177\u3002<\/li>\n<li><strong>SMOP<\/strong>\uff1a\u4e00\u4e2aMATLAB\u5230Python\u7684\u8f6c\u6362\u5668\u3002<\/li>\n<\/ol>\n<p><p>\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u4f60\u81ea\u52a8\u5c06MATLAB\u4ee3\u7801\u8f6c\u6362\u4e3aPython\u4ee3\u7801\uff0c\u4ece\u800c\u4f7f\u4f60\u80fd\u591f\u5728Python\u4e2d\u4f7f\u7528MATLAB\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5c06MATLAB\u7684\u51fd\u6570\u5bfc\u5165Python\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528MATLAB Engine API\u3001\u8f6c\u6362\u4e3aPython\u4ee3\u7801\u3001\u4f7f\u7528MATLAB Compiler SDK\u4ee5\u53ca\u4f7f\u7528\u7b2c\u4e09\u65b9\u5de5\u5177\u3002<strong>\u4f7f\u7528MATLAB Engine API<\/strong> \u662f\u4e00\u79cd\u975e\u5e38\u76f4\u63a5\u548c\u65b9\u4fbf\u7684\u65b9\u6cd5\uff0c\u5b83\u5141\u8bb8\u4f60\u5728Python\u4e2d\u76f4\u63a5\u8c03\u7528MATLAB\u51fd\u6570\u3002\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u4f60\u53ef\u4ee5\u4e86\u89e3\u5982\u4f55\u5b89\u88c5\u548c\u4f7f\u7528MATLAB Engine API\uff0c\u4ee5\u53ca\u5982\u4f55\u8c03\u7528\u81ea\u5b9a\u4e49\u7684MATLAB\u51fd\u6570\u3002\u5982\u679c\u9700\u8981\u66f4\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528MATLAB Compiler SDK \u6216\u7b2c\u4e09\u65b9\u5de5\u5177\u6765\u5b9e\u73b0MATLAB\u548cPython\u7684\u96c6\u6210\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8c03\u7528MATLAB\u51fd\u6570\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u8c03\u7528MATLAB\u51fd\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528MATLAB Engine API for Python\u3002\u8fd9\u4e00\u5de5\u5177\u5141\u8bb8\u4f60\u76f4\u63a5\u4ecePython\u811a\u672c\u4e2d\u542f\u52a8MATLAB\uff0c\u5e76\u6267\u884cMATLAB\u4ee3\u7801\u3002\u4f60\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86MATLAB\uff0c\u5e76\u4e14\u5728Python\u4e2d\u5b89\u88c5\u4e86MATLAB Engine API\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\uff1a\u5728Python\u73af\u5883\u4e2d\u5bfc\u5165MATLAB Engine\uff0c\u542f\u52a8\u5f15\u64ce\uff0c\u8c03\u7528MATLAB\u51fd\u6570\uff0c\u4f20\u9012\u8f93\u5165\u53c2\u6570\u5e76\u63a5\u6536\u8fd4\u56de\u503c\u3002\u8be6\u7ec6\u7684\u5b89\u88c5\u548c\u4f7f\u7528\u6307\u5357\u53ef\u4ee5\u5728MathWorks\u7684\u5b98\u65b9\u7f51\u7ad9\u4e0a\u627e\u5230\u3002<\/p>\n<p><strong>\u662f\u5426\u9700\u8981\u5c06MATLAB\u4ee3\u7801\u8f6c\u6362\u4e3aPython\u683c\u5f0f\u624d\u80fd\u4f7f\u7528\uff1f<\/strong><br \/>\u4e0d\u9700\u8981\u5c06MATLAB\u4ee3\u7801\u8f6c\u6362\u4e3aPython\u683c\u5f0f\u3002\u901a\u8fc7MATLAB Engine API\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u6267\u884cMATLAB\u4ee3\u7801\uff0c\u800c\u65e0\u9700\u5c06\u5176\u8f6c\u6362\u4e3aPython\u3002\u8fd9\u4e2a\u7279\u6027\u4f7f\u5f97\u4f7f\u7528\u73b0\u6709\u7684MATLAB\u51fd\u6570\u53d8\u5f97\u66f4\u52a0\u5bb9\u6613\uff0c\u7279\u522b\u662f\u5728\u4f60\u60f3\u8981\u5229\u7528MATLAB\u7684\u5f3a\u5927\u5de5\u5177\u7bb1\u548c\u51fd\u6570\u65f6\u3002<\/p>\n<p><strong>\u4f7f\u7528MATLAB\u51fd\u6570\u65f6\uff0c\u6027\u80fd\u4f1a\u53d7\u5230\u5f71\u54cd\u5417\uff1f<\/strong><br \/>\u8c03\u7528MATLAB\u51fd\u6570\u53ef\u80fd\u4f1a\u6709\u4e00\u5b9a\u7684\u6027\u80fd\u5f00\u9500\uff0c\u5c24\u5176\u662f\u5728\u9891\u7e41\u4ea4\u4e92\u65f6\u3002\u56e0\u4e3a\u6bcf\u6b21\u8c03\u7528MATLAB\u51fd\u6570\u65f6\uff0cPython\u548cMATLAB\u4e4b\u95f4\u4f1a\u8fdb\u884c\u6570\u636e\u4f20\u8f93\uff0c\u8fd9\u53ef\u80fd\u5bfc\u81f4\u5ef6\u8fdf\u3002\u5982\u679c\u9700\u8981\u8fdb\u884c\u5927\u91cf\u7684\u6570\u636e\u5904\u7406\uff0c\u8003\u8651\u5c06\u8ba1\u7b97\u5bc6\u96c6\u578b\u7684\u4efb\u52a1\u5c3d\u91cf\u96c6\u4e2d\u5728MATLAB\u4e2d\u6267\u884c\uff0c\u51cf\u5c11\u8de8\u8bed\u8a00\u7684\u8c03\u7528\u6b21\u6570\uff0c\u4ee5\u4f18\u5316\u6027\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u5c06MATLAB\u7684\u51fd\u6570\u5bfc\u5165Python \u5c06MATLAB\u7684\u51fd\u6570\u5bfc\u5165Python\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u4f7f\u7528MATLAB E [&hellip;]","protected":false},"author":3,"featured_media":1058564,"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\/1058554"}],"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=1058554"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1058554\/revisions"}],"predecessor-version":[{"id":1058569,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1058554\/revisions\/1058569"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1058564"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1058554"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1058554"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1058554"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}