4
$\begingroup$

I have multiple notebooks open, with similar variable declaration (am studying test cases), and I want all notebooks to be independent of each other, and also forget it's own evaluation after finishing. I have added

ClearAll["Global*"]

at the top of the notebook. Also done: Default Kernel as Local. Plus assigned Notebook's Default Context as Unique to this Notebook

But it still retains values.

I just tried adding Quit at the end of the notebook, but each time I Evaluate Notebook again (after it has done Quit once), my program crashes. I put Quit at the beginning of the notebook and did Evaluate Notebook but it compiled the first cell with Quit and then it stops there.

I am out of ideas, any suggestions?

I need this to be default - never having dependence of notebooks on each other and never remember values after ending one evaluation... I want it automated (right now, I have to do Evaluation > Quit Kernel after each evaluation...

$\endgroup$
4
  • 1
    $\begingroup$ Since you've asked different contexts to be assigned to each notebook, their variables are no longer in the Global` context, so ClearAll["Global*"]` is doing nothing. Try ClearAll[StringJoin[Context[], "*"]] instead. $\endgroup$
    – MarcoB
    Jun 1, 2023 at 12:00
  • $\begingroup$ I got error "ClearAll:: Context[]<>* is not a symbol or a string." with ClearAll[StringJoin[Context[], "*"]] but I tried with ClearAll[Evaluate[Context[] <> "*"]] and it works perfectly; thank you very much! $\endgroup$
    – ZeroTwo
    Jun 1, 2023 at 12:56
  • 1
    $\begingroup$ Great! I've summarized my comment and your modification in an answer below. $\endgroup$
    – MarcoB
    Jun 1, 2023 at 13:19
  • $\begingroup$ Possible duplicate: mathematica.stackexchange.com/questions/82803/… $\endgroup$
    – Michael E2
    Jun 1, 2023 at 15:23

2 Answers 2

2
$\begingroup$

Since you've asked for different contexts to be assigned to each notebook, their variables are no longer in the Global` context, so ClearAll["Global*"] is doing nothing for you. You can extract the name of the current context with Context[], then use ClearAll[Evaluate@ StringJoin[Context[], "*"]] instead.

$\endgroup$
4
$\begingroup$

Rather than try to clear all defined variables I like to just quit the kernel to get a clean slate.

To accomplish your goal I would place a single cell at the top of each notebook with the expression

Quit[]

and then just Cmd-A to select all cells and then evaluate them. First the kernel will quit and then all the other cells will evaluate. If you find that when it hits the Quit[] it no longer evaluates the other cells, then evaluate the following

CurrentValue[$FrontEnd, "ClearEvaluationQueueOnKernelQuit"] = False

I have no idea why that isn't the default value for that option.

$\endgroup$
4
  • 2
    $\begingroup$ Thanks for sharing this! One idea why it's the default: You want the kernel to stop, as in stop now, give me back control cuz I made a mistake. Another reason: the rest of the queue often depends on initialization, without which its execution fills the notebook with error messages. -- Alternatively they could have had two kinds of quit, like the C and AC buttons on old calculators. $\endgroup$
    – Michael E2
    Jun 1, 2023 at 15:22
  • $\begingroup$ That makes sense - I always considered the menu item Evaluate->Quit Kernel to be the "stop, stop now" command because it does it even when the kernel is hanging. I probably found ClearEvaluationQueueOnKernelQuit as an option shortly after I started using Mathematica so it seems natural to me. $\endgroup$
    – Jason B.
    Jun 1, 2023 at 19:00
  • $\begingroup$ Yeah, it's unlikely a user selects a sequence of cells in the middle of which is Quit[] and the following cells require initialization. That's their fault. And you can't abort by typing Quit[] in the middle of an evaluating queue. -- BTW, do you know what happens if ClearEvaluationQueueOnKernelQuit is false and you kill the kernel externally? Does evaluation restart? I guess you can always kill the FE if you do something stupid, but you lose unsaved work. $\endgroup$
    – Michael E2
    Jun 1, 2023 at 19:18
  • 2
    $\begingroup$ The Evaluation->Quit Kernel is a nuclear option, will not restart any computations after that $\endgroup$
    – Jason B.
    Jun 1, 2023 at 20:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.