Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
3  
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. –  István Zachar Nov 7 '12 at 17:51

2 Answers 2

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]], 
 "="]]]
share|improve this answer
    
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. –  rm -rf Nov 8 '12 at 16:43

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.

share|improve this answer
    
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. –  SixtySuit Nov 7 '12 at 19:42
    
@SixtySuit Uh? I don't understand what you mean... If you need Case1, then make Case1 a separate context. –  rm -rf Nov 8 '12 at 16:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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