protect

Protect an identifier

Use only in the MuPAD Notebook Interface.

This functionality does not run in MATLAB.

Syntax

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

Description

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).

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

Examples

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: The 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: The identifier 'important' is protected. [_assign]

The identifier keeps its previous value:

important

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":

protect(important)

important := 1
Warning: The 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: The protected variable 'x' is overwritten. [_assign]

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

Parameters

x

An identifier

Options

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.

Algorithms

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

Was this topic helpful?