Protect an identifier

MuPAD® notebooks will be removed in a future release. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.


protect(x, <ProtectLevelError | ProtectLevelWarning | ProtectLevelNone>)


protect(x) protects the identifier x.

protect(x, ProtectLevelError) sets full write-protection for the identifier. Any subsequent attempt to assign a value to the identifier will lead to an error.

protect(x, ProtectLevelWarning) sets a “soft” protection. Any subsequent assignment to the identifier results in a warning message. However, the identifier will be assigned a value, anyway.

protect(x) is equivalent to protect(x, ProtectLevelWarning).

protect(x, ProtectLevelNone) removes any protection from the identifier. This call is equivalent to unprotect(x).


Overwriting protected identifiers such as the names of MuPAD® functions may damage your current session.


Example 1

The following call protects the identifier important with the protection level “ProtectLevelWarning”:

protect(important, ProtectLevelWarning)

The identifier can still be overwritten:

important := 1
Warning: Protected variable 'important' is overwritten. [_assign]

We protect the identifier with the level “ProtectLevelError”:

protect(important, ProtectLevelError)

Now, it is no longer possible to overwrite important:

important := 2
Error: Identifier 'important' is protected. [_assign]

The identifier keeps its previous value:


In order to overwrite this value, we must unprotect important:

protect(important, ProtectLevelNone)

important := 2

The identifier is protected again with the default level “ProtectLevelWarning“:


important := 1
Warning: Protected variable 'important' is overwritten. [_assign]

unprotect(important): delete important:

Example 2

protect does not evaluate its first argument. Here the identifier x can still be overwritten, while its value – which is the identifier y – remains write protected:

protect(y, ProtectLevelError):  x := y:  protect(x):  x := 1
Warning: Protected variable 'x' is overwritten. [_assign]

y := 2
Error: Identifier 'y' is protected. [_assign]
unprotect(x):  unprotect(y):  delete x, y:



An identifier


ProtectLevelError, ProtectLevelNone, ProtectLevelWarning

The level of protection to set. The default value is ProtectLevelWarning.

Return Values

Previous protection level of x: either ProtectLevelError or ProtectLevelWarning or ProtectLevelNone.


protect does not evaluate its first argument. This way identifiers can be protected that have been assigned a value.

Identifiers starting with a # are implicitly protected and cannot be assigned a value nor receive assumptions.

See Also

MuPAD Functions