10
$\begingroup$

I'm trying to set up some notebooks so that I have one "seed" notebook that defines all my values globally when I run it. I have three other notebooks that take these values and run with them to do what they do.

Problem is, these notebooks solve for functions and define them locally. I can't re-run these notebooks without running the seed book every time since Solve sees functions that are already defined. Is there a way to clear JUST the variables I have defined in a notebook?

Edit: forgot to mention the seed notebook does a ClearAll["Global``*"] at the beginning.

$\endgroup$
1
  • 4
    $\begingroup$ I was trying to figure out what are "just variables"... Are these immanently just, or they become so when used by just programmers? I wonder who uses then the dishonest symbol table. $\endgroup$ Nov 7, 2012 at 17:51

2 Answers 2

12
$\begingroup$

The Global` context in a kernel is shared between notebooks communicating with that kernel, so issuing a ClearAll["Global`*"] in one notebook will clear them from all notebooks.

As an alternative, you can define your "seed" notebook variables in a specific context (e.g. seed`) and then add that context to the global path. For example, in your seed notebook,

Begin["Seed`"];
    foo = 1;
    bar = 2;
End[];

AppendTo[$ContextPath, "Seed`"];

Now you can use these variables in your secondary notebooks without having to expressly qualify them as Seed`foo and simply do ClearAll["Seed`*"] to clear only these variables.


You can also set the seed notebook to have a unique context by choosing Evaluation > Notebook's Default Context > Unique to this Notebook, which will then make its context something like Notebook$$12345` and you can append this to $ContextPath as above. However, having such a context name is both unsightly and not informative, so I'd suggest using explicitly (and appropriately) named contexts.

$\endgroup$
2
  • $\begingroup$ Hmmm... Sounds like I need to educate myself in the use of Contexts. Your suggestion is a good one, but from what I can tell it's doing the opposite of what I need--allowing me to clear Seed when I want to clear, say, Case1 for my notebook test case. Thanks though; it's a good start. $\endgroup$
    – SixtySuit
    Nov 7, 2012 at 19:42
  • $\begingroup$ @SixtySuit Uh? I don't understand what you mean... If you need Case1, then make Case1 a separate context. $\endgroup$
    – rm -rf
    Nov 8, 2012 at 16:46
2
$\begingroup$

It's a bit of a workaround but you can:

1) list all the Global variables like so: Names["Global`*"]

2) search you current notebook for specific strings

By searching your notebook, returning a list of all the positions where global variables are used and then searching for which instances there is an "=" directly after that variable you can get a list of all variables defined in the current notebook:

Take[Names["Global`*"], 
 Flatten[Position[{Off[StringTake::mseqs]; 
   StringTake[
      ToString[
       FindList[
        NotebookFileName[], #]], {Max[Flatten[
          StringPosition[FindList[NotebookFileName[], #], #]]] + 6}]
         & /@ Names["Global`*"], On[StringTake::mseqs]}[[1]], 
 "="]]]
$\endgroup$
1
  • 1
    $\begingroup$ It's a decent start, but there are several ways to define variables. For instance, :=, /:, :^=, etc. You can also define several variables at once, as {foo, bar, baz} = ... Also, there's the possibility of strings such as "foo = ..." showing up as false positives. $\endgroup$
    – rm -rf
    Nov 8, 2012 at 16:43

Your Answer

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

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