Jupyter和MATLAB关联:windows系统

Jupyter和MATLAB关联:windows系统

作者:韩少真(西北大学) || 展金永(对外经济贸易大学)

1. 准备工作-关联前提

首先,请按助教提供教程安装Anaconda和MATLAB,并检验Anaconda和MATLAB是否已经成功安装在本机电脑上。

2. 安装matlab的python拓展

2.1 获取扩展的路径

打开matlab的安装路径(如果忘记可以看下matlab安装教程的第六步),依次打开extern、engines、python文件夹,进入python文件夹。然后点击地址栏,复制python文件夹的路径地址,如下图所示:


我的电脑为D:\Polyspace\R2019a\extern\engines\python,根据matlab安装的地址不同,D:\Polyspace\R2019a\可能有所差异,请根据个人安装状况更改。

2.2 安装MATLAB的Python扩展

  • 通过开始-Anaconda3(64-bit)-Anaconda Powershell prompt,打开Anaconda Powershell prompt。如果直接打开执行下面的操作无法正常关联python和matlab,则需要右击选择以管理员身份运行,重新进行接下来的操作。
  • 在powershell中依次输入以下命令并按回车键执行
cd "D:\Polyspace\R2019a\extern\engines\python"
python setup.py install

-执行结果如下所示,表明扩展安装成功

(base) PS C:\Users\mailshaozhen> cd D:\Polyspace\R2019a\extern\engines\python
(base) PS D:\Polyspace\R2019a\extern\engines\python> python setup.py install
running install
running build
running build_py
creating build
creating build\lib
creating build\lib\matlab
copying dist\matlab\mlarray.py -> build\lib\matlab
copying dist\matlab\mlexceptions.py -> build\lib\matlab
copying dist\matlab\__init__.py -> build\lib\matlab
creating build\lib\matlab\engine
copying dist\matlab\engine\basefuture.py -> build\lib\matlab\engine
copying dist\matlab\engine\engineerror.py -> build\lib\matlab\engine
copying dist\matlab\engine\enginehelper.py -> build\lib\matlab\engine
copying dist\matlab\engine\enginesession.py -> build\lib\matlab\engine
copying dist\matlab\engine\fevalfuture.py -> build\lib\matlab\engine
copying dist\matlab\engine\futureresult.py -> build\lib\matlab\engine
copying dist\matlab\engine\matlabengine.py -> build\lib\matlab\engine
copying dist\matlab\engine\matlabfuture.py -> build\lib\matlab\engine
copying dist\matlab\engine\__init__.py -> build\lib\matlab\engine
creating build\lib\matlab\_internal
copying dist\matlab\_internal\mlarray_sequence.py -> build\lib\matlab\_internal
copying dist\matlab\_internal\mlarray_utils.py -> build\lib\matlab\_internal
copying dist\matlab\_internal\__init__.py -> build\lib\matlab\_internal
running install_lib
creating D:\Anaconda3\Lib\site-packages\matlab
creating D:\Anaconda3\Lib\site-packages\matlab\engine
copying build\lib\matlab\engine\basefuture.py -> D:\Anaconda3\Lib\site-packages\matlab\engine
copying build\lib\matlab\engine\engineerror.py -> D:\Anaconda3\Lib\site-packages\matlab\engine
copying build\lib\matlab\engine\enginehelper.py -> D:\Anaconda3\Lib\site-packages\matlab\engine
copying build\lib\matlab\engine\enginesession.py -> D:\Anaconda3\Lib\site-packages\matlab\engine
copying build\lib\matlab\engine\fevalfuture.py -> D:\Anaconda3\Lib\site-packages\matlab\engine
copying build\lib\matlab\engine\futureresult.py -> D:\Anaconda3\Lib\site-packages\matlab\engine
copying build\lib\matlab\engine\matlabengine.py -> D:\Anaconda3\Lib\site-packages\matlab\engine
copying build\lib\matlab\engine\matlabfuture.py -> D:\Anaconda3\Lib\site-packages\matlab\engine
copying build\lib\matlab\engine\_arch.txt -> D:\Anaconda3\Lib\site-packages\matlab\engine
copying build\lib\matlab\engine\__init__.py -> D:\Anaconda3\Lib\site-packages\matlab\engine
copying build\lib\matlab\mlarray.py -> D:\Anaconda3\Lib\site-packages\matlab
copying build\lib\matlab\mlexceptions.py -> D:\Anaconda3\Lib\site-packages\matlab
creating D:\Anaconda3\Lib\site-packages\matlab\_internal
copying build\lib\matlab\_internal\mlarray_sequence.py -> D:\Anaconda3\Lib\site-packages\matlab\_internal
copying build\lib\matlab\_internal\mlarray_utils.py -> D:\Anaconda3\Lib\site-packages\matlab\_internal
copying build\lib\matlab\_internal\__init__.py -> D:\Anaconda3\Lib\site-packages\matlab\_internal
copying build\lib\matlab\__init__.py -> D:\Anaconda3\Lib\site-packages\matlab
byte-compiling D:\Anaconda3\Lib\site-packages\matlab\engine\basefuture.py to basefuture.cpython-37.pyc
byte-compiling D:\Anaconda3\Lib\site-packages\matlab\engine\engineerror.py to engineerror.cpython-37.pyc
byte-compiling D:\Anaconda3\Lib\site-packages\matlab\engine\enginehelper.py to enginehelper.cpython-37.pyc
byte-compiling D:\Anaconda3\Lib\site-packages\matlab\engine\enginesession.py to enginesession.cpython-37.pyc
byte-compiling D:\Anaconda3\Lib\site-packages\matlab\engine\fevalfuture.py to fevalfuture.cpython-37.pyc
byte-compiling D:\Anaconda3\Lib\site-packages\matlab\engine\futureresult.py to futureresult.cpython-37.pyc
byte-compiling D:\Anaconda3\Lib\site-packages\matlab\engine\matlabengine.py to matlabengine.cpython-37.pyc
byte-compiling D:\Anaconda3\Lib\site-packages\matlab\engine\matlabfuture.py to matlabfuture.cpython-37.pyc
byte-compiling D:\Anaconda3\Lib\site-packages\matlab\engine\__init__.py to __init__.cpython-37.pyc
byte-compiling D:\Anaconda3\Lib\site-packages\matlab\mlarray.py to mlarray.cpython-37.pyc
byte-compiling D:\Anaconda3\Lib\site-packages\matlab\mlexceptions.py to mlexceptions.cpython-37.pyc
byte-compiling D:\Anaconda3\Lib\site-packages\matlab\_internal\mlarray_sequence.py to mlarray_sequence.cpython-37.pyc
byte-compiling D:\Anaconda3\Lib\site-packages\matlab\_internal\mlarray_utils.py to mlarray_utils.cpython-37.pyc
byte-compiling D:\Anaconda3\Lib\site-packages\matlab\_internal\__init__.py to __init__.cpython-37.pyc
byte-compiling D:\Anaconda3\Lib\site-packages\matlab\__init__.py to __init__.cpython-37.pyc
running install_egg_info
Writing D:\Anaconda3\Lib\site-packages\matlabengineforpython-R2018a-py3.7.egg-info
(base) PS D:\Polyspace\R2019a\extern\engines\python>

3.在Python中安装matlab_kernel包

  • 根据下图,打开Anaconda prompt
  • Anaconda prompt中运行以下代码
pip install matlab_kernel
  • anaconda
    会自动安装matlab_kernel包,具体如下所示,则安装成功
(base) C:\Users\mailshaozhen>pip install matlab_kernel
Collecting matlab_kernel
  Downloading https://files.pythonhosted.org/packages/64/ad/5f471160ec33e2f0f3586d285cd5e3b6dff51027849f28369d43d1d57fd1/matlab_kernel-0.16.7-py3-none-any.whl
Requirement already satisfied: ipython>=4.0.0 in d:\anaconda3\lib\site-packages (from matlab_kernel) (7.4.0)
Collecting metakernel>=0.23.0 (from matlab_kernel)
  Downloading https://files.pythonhosted.org/packages/bb/bd/658103e652c2c12a791c83539496541e8f8d755c1cc3293861983f0a6742/metakernel-0.24.2-py2.py3-none-any.whl (208kB)
    100% |████████████████████████████████| 215kB 13kB/s
Requirement already satisfied: jupyter-client>=4.4.0 in d:\anaconda3\lib\site-packages (from matlab_kernel) (5.2.4)
Requirement already satisfied: jedi>=0.10 in d:\anaconda3\lib\site-packages (from ipython>=4.0.0->matlab_kernel) (0.13.3)
Requirement already satisfied: backcall in d:\anaconda3\lib\site-packages (from ipython>=4.0.0->matlab_kernel) (0.1.0)
Requirement already satisfied: prompt-toolkit<2.1.0,>=2.0.0 in d:\anaconda3\lib\site-packages (from ipython>=4.0.0->matlab_kernel) (2.0.9)
Requirement already satisfied: traitlets>=4.2 in d:\anaconda3\lib\site-packages (from ipython>=4.0.0->matlab_kernel) (4.3.2)
Requirement already satisfied: colorama; sys_platform == "win32" in d:\anaconda3\lib\site-packages (from ipython>=4.0.0->matlab_kernel) (0.4.1)
Requirement already satisfied: setuptools>=18.5 in d:\anaconda3\lib\site-packages (from ipython>=4.0.0->matlab_kernel) (40.8.0)
Requirement already satisfied: decorator in d:\anaconda3\lib\site-packages (from ipython>=4.0.0->matlab_kernel) (4.4.0)
Requirement already satisfied: pygments in d:\anaconda3\lib\site-packages (from ipython>=4.0.0->matlab_kernel) (2.3.1)
Requirement already satisfied: pickleshare in d:\anaconda3\lib\site-packages (from ipython>=4.0.0->matlab_kernel) (0.7.5)
Collecting ipyparallel (from metakernel>=0.23.0->matlab_kernel)
  Downloading https://files.pythonhosted.org/packages/3f/82/aaa7a357845a98d4028f27c799f0d3bb2fe55fc1247c73dc712b4ae2344c/ipyparallel-6.2.4-py2.py3-none-any.whl (198kB)
    100% |████████████████████████████████| 204kB 7.5kB/s
Requirement already satisfied: ipykernel in d:\anaconda3\lib\site-packages (from metakernel>=0.23.0->matlab_kernel) (5.1.0)
Collecting pexpect>=4.2 (from metakernel>=0.23.0->matlab_kernel)
  Downloading https://files.pythonhosted.org/packages/0e/3e/377007e3f36ec42f1b84ec322ee12141a9e10d808312e5738f52f80a232c/pexpect-4.7.0-py2.py3-none-any.whl (58kB)
    100% |████████████████████████████████| 61kB 10kB/s
Requirement already satisfied: tornado>=4.1 in d:\anaconda3\lib\site-packages (from jupyter-client>=4.4.0->matlab_kernel) (6.0.2)
Requirement already satisfied: jupyter-core in d:\anaconda3\lib\site-packages (from jupyter-client>=4.4.0->matlab_kernel) (4.4.0)
Requirement already satisfied: pyzmq>=13 in d:\anaconda3\lib\site-packages (from jupyter-client>=4.4.0->matlab_kernel) (18.0.0)
Requirement already satisfied: python-dateutil>=2.1 in d:\anaconda3\lib\site-packages (from jupyter-client>=4.4.0->matlab_kernel) (2.8.0)
Requirement already satisfied: parso>=0.3.0 in d:\anaconda3\lib\site-packages (from jedi>=0.10->ipython>=4.0.0->matlab_kernel) (0.3.4)
Requirement already satisfied: wcwidth in d:\anaconda3\lib\site-packages (from prompt-toolkit<2.1.0,>=2.0.0->ipython>=4.0.0->matlab_kernel) (0.1.7)
Requirement already satisfied: six>=1.9.0 in d:\anaconda3\lib\site-packages (from prompt-toolkit<2.1.0,>=2.0.0->ipython>=4.0.0->matlab_kernel) (1.12.0)
Requirement already satisfied: ipython-genutils in d:\anaconda3\lib\site-packages (from traitlets>=4.2->ipython>=4.0.0->matlab_kernel) (0.2.0)
Collecting ptyprocess>=0.5 (from pexpect>=4.2->metakernel>=0.23.0->matlab_kernel)
  Downloading https://files.pythonhosted.org/packages/d1/29/605c2cc68a9992d18dada28206eeada56ea4bd07a239669da41674648b6f/ptyprocess-0.6.0-py2.py3-none-any.whl
Installing collected packages: ipyparallel, ptyprocess, pexpect, metakernel, matlab-kernel
Successfully installed ipyparallel-6.2.4 matlab-kernel-0.16.7 metakernel-0.24.2 pexpect-4.7.0 ptyprocess-0.6.0
  • 然后在Anaconda prompt中输入jupyter notebook,回车执行就会直接打开jupyter notebook。打开jupyter notebook的方法有很多,可以在开始程序中找到jupyter notebook点击打开,也可以到Anaconda的主界面打开jupyter notebook。根据下图步骤新建Matlab的文档。

  • 在打开的notebook中输入matlab语句,例如a=3,表示生成新变量a,并给其赋值为3。点击运行,出现运行结果,表明关联成功。具体如下图所示:

  • 注意:执行第一条指令的时候要耐心等一等,matlab r2019a程序比较大,启动比较慢,执行完第一行执行,接下来执行就会快很多,可以继续执行b=a,测试发现基本不用等待就会出结果。

    image.png

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,240评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,328评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,182评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,121评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,135评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,093评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,013评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,854评论 0 273
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,295评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,513评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,678评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,398评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,989评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,636评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,801评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,657评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,558评论 2 352