Copy Variables and Expressions Between MATLAB and MuPAD

You can copy a variable from a MuPAD® notebook to a variable in the MATLAB® workspace using a MATLAB command. Similarly, you can copy a variable or symbolic expression in the MATLAB workspace to a variable in a MuPAD notebook using a MATLAB command. To do either assignment, you need to know the handle to the MuPAD notebook you want to address.

The only way to assign variables between a MuPAD notebook and the MATLAB workspace is to open the notebook using the following syntax:

nb = mupad;

You can use any variable name for the handle nb. To open an existing notebook file, use the following syntax:

nb = mupad(file_name);

Here file_name must be a full path unless the notebook is in the current folder. The handle nb is used only for communication between the MATLAB workspace and the MuPAD notebook.

  • To copy a symbolic variable in the MATLAB workspace to a variable in the MuPAD notebook engine with the same name, enter this command in the MATLAB Command Window:

    setVar(notebook_handle,'MuPADvar',MATLABvar)

    For example, if nb is the handle to the notebook and z is the variable, enter:

    setVar(nb,'z',z)

    There is no indication in the MuPAD notebook that variable z exists. To check that it exists, enter the command anames(All, User) in the notebook.

  • To assign a symbolic expression to a variable in a MuPAD notebook, enter:

    setVar(notebook_handle,'variable',expression)

    at the MATLAB command line. For example, if nb is the handle to the notebook, exp(x) - sin(x) is the expression, and z is the variable, enter:

    syms x
    setVar(nb,'z',exp(x) - sin(x))

    For this type of assignment, x must be a symbolic variable in the MATLAB workspace.

    Again, there is no indication in the MuPAD notebook that variable z exists. Check that it exists by entering this command in the notebook:

    anames(All, User)
  • To copy a symbolic variable in a MuPAD notebook to a variable in the MATLAB workspace, enter in the MATLAB Command Window:

    MATLABvar = getVar(notebook_handle,'variable');

    For example, if nb is the handle to the notebook, z is the variable in the MuPAD notebook, and u is the variable in the MATLAB workspace, enter:

    u = getVar(nb,'z')

    Communication between the MATLAB workspace and the MuPAD notebook occurs in the notebook's engine. Therefore, variable z must be synchronized into the notebook's MuPAD engine before using getVar, and not merely displayed in the notebook. If you try to use getVar to copy an undefined variable z in the MuPAD engine, the resulting MATLAB variable u is empty. For details, see Evaluate MuPAD Notebooks from MATLAB.

    Tip   Do all copying and assignments from the MATLAB workspace, not from a MuPAD notebook.

Copy and Paste Using the System Clipboard

You can also copy and paste between notebooks and the MATLAB workspace using standard editing commands. If you copy a result in a MuPAD notebook to the system clipboard, you might get the text associated with the expression, or a picture, depending on your operating system and application support.

For example, consider this MuPAD expression:

Select the output with the mouse and copy it to the clipboard:

Paste this into the MATLAB workspace. The result is text:

exp(x)/(x^2 + 1)

If you paste it into Microsoft® WordPad on a Windows® system, the result is a picture.

Was this topic helpful?