在Jupyter Notebooks和JupyterLab中使用Matlab

2022-01-18 11:16:56 浏览数 (1)

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.

0 人点赞