| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Symbolic Math Toolbox |
| Contents | Index |
| Learn more about Symbolic Math Toolbox |
| On this page… |
|---|
Copying Variables and Expressions Between the MATLAB Workspace and MuPAD Notebooks |
You can copy a variable in 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. In order 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 start the notebook using the syntax
nb = mupad;
(you can use any variable name for the handle nb) or open an existing notebook file with
nb = mupad(file_name);
Here file_name must be a full path unless the notebook is in the current folder. The nb handle 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
setVar(notebook_handle,variable)
at the MATLAB command line. For example, if nb is the handle to the notebook and z is the variable, enter
setVar(nb,z)
There is no indication in the MuPAD notebook that the variable z exists. Check that it exists by entering z in an input area of the notebook, or by entering 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 the variable z exists. Check that it exists by entering z in an input area of the notebook, or by entering the command anames(All, User) in the notebook.
To copy a symbolic variable in a MuPAD notebook to a variable in the MATLAB workspace, enter
MATLABvar = getVar(notebook_handle,'variable');
at the MATLAB command line. 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')
The communication between the MATLAB workspace and the MuPAD notebook takes place with the notebook's engine. Therefore, the 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 a variable z that is undefined in the MuPAD engine, the resulting MATLAB variable u is empty. For more information on this topic, see Synchronizing a Notebook and its Engine.
Note All such copying and assignment must be done from the MATLAB workspace, not from a MuPAD notebook. |

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 may get the text associated with the expression, or a picture, depending on your operating system and application support.
For example, consider the following 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 WordPad on a Windows® system, the result is a picture.

To access MuPAD functions and procedures at the MATLAB command line, use the evalin(symengine,...) function or the feval(symengine,...) function. These functions are designed to work like the existing MATLAB functions evalin and feval.
Note You cannot use evalin and feval to access the MuPAD function log that represents the logarithm to an arbitrary base. Instead, both commands evaluate the natural logarithm. |
For evalin, the syntax is
y = evalin(symengine,'MuPAD_Expression');
Use evalin when you want to perform computations in the MuPAD language, while working in the MATLAB workspace.
For example, to make a three-element symbolic vector of the sin(kx) function, k = 1 to 3, enter
y = evalin(symengine,'sin(k*x) $ k = 1..3')
The result is
y = sin(x), sin(2*x), sin(3*x)
For evaluating a MuPAD function, you can also use the feval function. feval has a different syntax than evalin, so it can be simpler to use. The syntax is
y = feval(symengine,'MuPAD_Function',x1,...,xn);
MuPAD_Function represents the name of a MuPAD function. The arguments x1,...,xn must be symbolic variables, numbers, or strings. For example, to find the tenth element in the Fibonacci sequence, enter
z = feval(symengine,'numlib::fibonacci',10)
The result is
z = 55
The next example compares the use of a symbolic solution of an equation to the solution returned by the MuPAD numeric fsolve function near the point x = 3. For information on this function, enter doc(symengine,'numeric::fsolve') at the MATLAB command line. The symbolic solver
syms x f = sin(x^2); solve(f)
returns
ans = 0 0
The numeric solver fsolve
feval(symengine, 'numeric::fsolve',f,'x=3')
returns
ans = [x = 3.0699801238394654654386548746677946]
As you might expect, the answer is the numerical value of
. The setting of MATLAB format does
not affect the display; it is the full returned value from the MuPAD 'numeric::fsolve' function.
The evalin(symengine,...) function causes the MuPAD engine to evaluate a string. Since the MuPAD engine workspace is generally empty, expressions returned by evalin(symengine,...) are not simplified or evaluated according to their definitions in the MATLAB workspace.
For example:
syms x y = x^2; evalin(symengine, 'cos(y)')
ans = cos(y)
The variable y is not expressed in terms of x because y is not known to the MuPAD engine workspace.
In contrast, feval(symengine,...) can pass symbolic variables that exist in the MATLAB workspace, and these variables are evaluated before being processed in the MuPAD engine. For example:
syms x y = x^2; feval(symengine,'cos',y)
ans = cos(x^2)
The symbolic engine workspace associated with the MATLAB workspace is usually empty. The MATLAB workspace keeps track of the values of symbolic variables, and passes them to the symbolic engine for evaluation as necessary. However, the symbolic engine workspace contains all assumptions you make about symbolic variables, such as whether a variable is real or positive. These assumptions can affect solutions to equations, simplifications, and transformations, as explained in Examples of the Effect of Assumptions.
Note The commands syms x
x = sym('x')
clear xclear any existing value of x in the MATLAB workspace, but do not clear assumptions about x in the symbolic engine workspace. |
If you make an assumption about the nature of a variable, e.g., by the commands
syms x real
or
syms x positive
clearing the variable x from the MATLAB workspace does not clear the assumption from the symbolic engine workspace. To clear the assumption, enter the command
syms x clear
For more detail, see Checking a Variable's Assumptions and Examples of the Effect of Assumptions.
If you reset the symbolic engine by entering the command
reset(symengine)
or if you change symbolic engines with the symengine command, MATLAB no longer recognizes any symbolic variables that exist in the MATLAB workspace. Clear the variables with the clear command, or renew them with the syms or sym commands.
Here is an example of how the MATLAB workspace and the symbolic engine workspace respond to a sequence of commands:
| Step | Command | MATLAB workspace | MuPAD Engine Workspace |
|---|---|---|---|
| 1 | syms x positive | x | x is positive |
| 2 | clear x | empty | x is positive |
| 3 | syms x | x | x is positive |
| 4 | syms x clear | x | empty |
To check whether a variable, say x, has any assumption in the symbolic engine workspace associated with the MATLAB workspace, enter the command
evalin(symengine,'getprop(x)')
at the MATLAB command line.
If the returned answer is C_, there are no assumptions about the variable. (C_ means it can be any complex number.)
If the returned value is anything else, there are assumptions about the variable.
For example:
syms x real evalin(symengine,'getprop(x)')
ans = R_
syms x clear
syms z evalin(symengine,'assume(z <> 0)') evalin(symengine,'getprop(z)')
ans =
C_ minus {0}syms z clear evalin(symengine,'getprop(z)')
ans = C_
For more information about the basic sets that can be returned as assumptions, enter
doc(symengine,'solvelib::BasicSet')
Assumptions can change the answers that the solve function returns, and can change the results of simplifications. The only assumptions you can make using MATLAB commands are real or positive.
For example, consider what transpires when solving the equation x^3 = 1:
syms x
solve('x^3 = 1')ans =
1
- (3^(1/2)*i)/2 - 1/2
(3^(1/2)*i)/2 - 1/2syms x real
solve('x^3 = 1')ans = 1
However, clearing x does not change the underlying assumption that x is real:
clear x
syms x
solve('x^3 = 1')ans = 1
Clearing x with syms x clear clears the assumption:
syms x clear
solve('x^3 = 1')ans =
1
- (3^(1/2)*i)/2 - 1/2
(3^(1/2)*i)/2 - 1/2Using evalin or feval, you can make a variety of assumptions about an expression; see Calling MuPAD Functions at the MATLAB Command Line. All such assumptions can be cleared with the command syms x clear, as in this example:
evalin(symengine,'assume(a <> 0)'); evalin(symengine,'solve(a*x^2 + b*x + c = 0,x)')
ans =
{-(b - (b^2 - 4*a*c)^(1/2))/(2*a),...
-(b + (b^2 - 4*a*c)^(1/2))/(2*a)}syms a clear evalin(symengine,'solve(a*x^2 + b*x + c = 0,x)')
ans =
piecewise([a <> 0, {-(b - (b^2 - 4*a*c)^(1/2))/(2*a),...
-(b + (b^2 - 4*a*c)^(1/2))/(2*a)}],...
[a = 0 and b <> 0, {-c/b}], [a = 0 and b = 0 and c = 0, C_],...
[a = 0 and b = 0 and c <> 0, {}])
![]() | MuPAD for MATLAB Users | Function Reference | ![]() |

See how symbolic computations can help you find analytical solutions to math and engineering problems.
Get free kit| © 1984-2010- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |