前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >在Jupyter Notebooks和JupyterLab中使用Matlab

在Jupyter Notebooks和JupyterLab中使用Matlab

作者头像
气象学家
发布2022-01-18 11:16:56
8050
发布2022-01-18 11:16:56
举报
文章被收录于专栏:气象学家

http://www.jmlilly.net/jupyter-matlab

Using Jupyter Notebooks and JupyterLab with Matlab

April 11, 2021

Jupyter Notebooks offer a powerful and widely used platform for creating interactive scripts and journals. {JupyterLab} is the next-generation environment for Jupyter Notebooks that includes, among other things, a tabbed interface for multiple notebooks. Here, we will set these up to work with Matlab.

While Matlab’s Live Scripts offer a similar functionality, in my experience these are too buggy and unstable to be satisfactory. It takes a little work to set up Jupyter Notebooks to work with Matlab, but the payoff is worth it.

These instructions are taken from {here}, with some minor modifications and updates. They are written for a Mac or Linux operating system; the Windows instructions should be the same apart from obvious changes for directory changing commands and pathnames. More details on the Window installation are given in the {original instructions}.

If don’t already have it installed, you’ll need to install either the {Anaconda} or {Miniconda} Python distributions. Personally, I prefer the lean Miniconda distribution because it is a much faster install. Miniconda currently has versions for Python 2.7, 3.8, and 3.9. The version doesn’t particularly matter, so I chose the Python 3.9 version.

After Anaconda or Miniconda is installed, we’ll create a virtual environment, called jmatlab, that specifies a suitable version of Python to work with Matlab. For the current release of Matlab at the time of this writing, R2021a, the most recent compatible version of Python is 3.8.8. Use

代码语言:javascript
复制
conda create -vv -n jmatlab python=3.8.8 jupyter

to create this environment. If you’re running a different version of Matlab, check [this list] to find the compatible versions of Python.

Now, activate the jmatlab environment

代码语言:javascript
复制
conda activate jmatlab

which puts you inside this environment, as your shell cursor probably indicates. All the remaining commands should be executed from within this environment.

Install {JupyterLab} using the command

代码语言:javascript
复制
conda install -c conda-forge jupyterlab

and then install the Matlab kernel with

代码语言:javascript
复制
pip install matlab_kernel
python -m matlab_kernel install

where the pip command corrects a typo in the {original instructions}. To check that the kernel is installed correctly, use

代码语言:javascript
复制
jupyter kernelspec list

and you should see Matlab on the list of available kernels.

There is one more step that needs to be done on the Matlab side. Still inside the jmatlab environment, type

代码语言:javascript
复制
cd /Applications/MATLAB_R2021a.app/extern/engines/python
python setup.py install

which will allow the Matlab engine to be called from within a Python session.

Now, you’re ready to launch JupyterLab. Change to the directory you’d like to work in and type

代码语言:javascript
复制
jupyter-lab

or, alternatively, use

代码语言:javascript
复制
jupyter notebook

if you wish to start Jupyter Notebook without JupyterLab.

You should now be able to launch a Matlab notebook. You can do this by clicking on the “+” menu at the upper left if using JupyterLab, or, if you’re using Jupyter Notebook, from the “New” dropdown menu on the upper right.

To check that Matlab is working correctly, try entering surf(peaks) at the command prompt and then pressing play. For this first command, there will be a long pause before the plot appears, but after that it should work more speedily.

The next time you want to use JupyterLab, you only need to activate the jmatlab environment with “conda activate jmatlab” and then use “jupyter-lab” to launch it.

The JupyterLab interface is fairly self-evident, but more details can be found in its {documentation}. A great reference for working with the Markdown format of the text cells can be in {this tutorial}.

Please {contact me} if you find any inaccuracies or omissions in these instructions.

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-04-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 气象学家 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Using Jupyter Notebooks and JupyterLab with Matlab
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档