You can use magic commands with the MATLAB kernel. You can use the predefined magic commands in this folder, and you can implement your own by following the steps below.
Magic commands for the MATLAB kernel are prefixed with two percentage symbols %%
without whitespaces. For example, to list available magic commands, run %%lsmagic
Note that magic commands will only work at the beginning of cells, and will not work with MATLAB variables.
The magic commands help
and file
accept additional parameters. For example, to display information about a magic command, run %%help
followed by the name of the magic as an argument: %%help time
This table lists the predefined magic commands you can use:
Name | Description | Additional Parameters | Constraints | Example command |
---|---|---|---|---|
lsmagic | List predefined magic commands. | %%lsmagic |
||
help | Display information about provided magic command. | Name of magic command. | %%help file |
|
time | Display time taken to execute a cell. | %%time |
||
file | Save contents of cell as a file in the notebook folder. You can use this command to define and save new functions. For details, see the section below on how to Create New Functions Using the %%file Magic Command | Name of saved file | The file magic command will save the contents of the cell, but not execute them in MATLAB | %%file myfile.m |
To request a new magic command, create an issue.
To implement your own magic commands, follow these steps. You can use the predefined magic commands as examples.
- In the
magics
folder, create a Python file with the name of your new magic command, for example<new_magic_name>.py
. - Create a child class that inherits from the
MATLABMagic
class located injupyter_matlab_kernel/magics/base/matlab_magic.py
and modify these function members:- info_about_magic
- skip_matlab_execution
- before_cell_execute
- after_cell_execute
- do_complete
For details about these fields, see the descriptions in the
MATLABMagic
class.
- Add tests for your magic command in the
tests/unit/jupyter_matlab_kernel/magics
folder.
In a notebook cell you can define MATLAB functions that are scoped to that cell. To define a function scoped to all the cells in a notebook, you can use the %%file
magic command. Define a new function and save it as a MATLAB .m
file, using the name of the function as the file name. For example, to create a function called myAdditionFunction(x, y)
, follow these steps:
-
In a notebook cell, use the
%%file
command and define the function.%%file myAdditionFunction.m function addition = myAdditionFunction(x, y) addition = x + y; end
-
Run the cell to create a file called
myAdditionFunction.m
in the same folder as your notebook. -
You can then use this function in other cells of the notebook.
addition = myAdditionFunction(3, 4); disp(addition)
Note: to use your function in MATLAB, remember to add the Jupyter notebook folder to the MATLAB path.
Copyright 2024 The MathWorks, Inc.