Unlike the function Clear, the function Unset does not work for string patterns. Given the name of a variable as a string, how can you unset the corresponding variable? Example:
Given:
x = 10;
trying to unset later:
Unset[Symbol["x"]]
results in
Unset::write: Tag Symbol in Symbol[x] is Protected. >>
Using With
:
With[{var = Symbol["x"]}, Unset[var]]
results in
Unset::usraw: Cannot unset raw object 10. >>
Note that Clear["x"]
is not an option, because it also removes all DownValues
associated with x
.
ToExpression["Unset[" <> "x" <> "]"]
. $\endgroup$ – J. M.'s ennui♦ May 7 '13 at 12:27ToExpression@MakeBoxes[RawBoxes["x"] =.]
$\endgroup$ – Kuba♦ May 13 '15 at 12:37