Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Protect Function and Option Names

MuPAD® notebooks are not recommended. 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.

The names of the built-in MuPAD® functions and options are protected. If you try to assign a value to a MuPAD function or option, the system throws an error. This approach ensures that you will not overwrite a built-in function or option accidentally.

If you create a new MuPAD procedure, it is recommended to protect the procedure and all its options, especially if you often use that procedure. For example, MuPAD does not provide a function for computing Lucas numbers. You can write your own procedure for computing Lucas numbers, and then protect the procedure name.

The Lucas numbers are a sequence of integers. The recursion formula that defines the nth Lucas number is similar to the definition of the Fibonacci numbers:

Create the following procedure that computes the nth Lucas number:

lucas:= proc(n:Type::PosInt)
  option remember;
begin
  if n = 1 then
    1
  elif n = 2 then
    3
  else
    lucas(n - 1) + lucas(n - 2)
  end_if
end_proc:
lucas(i) $ i = 1..5

Now protect the procedure name, lucas, using protect with the ProtectLevelError option:

protect(lucas, ProtectLevelError):

ProtectLevelError lets you set full protection for the identifier. Now, trying to assign any value to lucas results in error:

lucas := 0
Error: Identifier 'lucas' is protected. [_assign]

Alternatively, you can use the ProtectLevelWarning option. In this case, you can still assign a value to the protected identifier, but a warning appears, for example:

protect(lucas, ProtectLevelWarning):

You can assign any value to lucas now, but such assignment triggers a warning:

lucas := 0
Warning: Protected variable 'lucas' is overwritten. [_assign]

For further computations, remove protection from the identifier lucas:

unprotect(lucas):
Was this topic helpful?