0
$\begingroup$

This question already has an answer here:

I've got a code that I want to automate, but I'm not sure how. I have a Mathematica notebook that I need to run many times over, each time changing one variable and repeating. My idea was to set up a "parent" notebook with a For loop that changes the variable each time, and the main notebook is called each time inside the For loop. However, I think this involves parallel Mathematica computing, and I'm new to this. I tried to tell the "parent" notebook to Pause[] while the other notebook is running, but that pauses both notebooks and nothing gets done. Help?!

Here's some example code:

For[i=1,i<7,i++,
 For[j=1,j<7,j++,

(* lots of inputs dependent on i and j *)

nb=NotebookOpen["Sanple.nb"]; SelectionMove[nb,All,Notebook]; SelectionEvaluate[nb];

];];

What I need my code to do is to open Sample.nb, evaluate it for the current i and j, and then wait for that evaluation to be done before moving on to the next i and j. Sample.nb is the core algorithm that I need to run many times (gonna be hundreds), so this loop will definitely help out.

$\endgroup$

marked as duplicate by Yves Klett, MarcoB, m_goldberg, Michael E2, user9660 Jul 11 '16 at 19:39

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ I think a more concrete example will help us greatly in helping you. $\endgroup$ – yohbs Feb 1 '15 at 21:10
  • $\begingroup$ If you can give a minimum example of what you seek, we can help you. Nevertheless, from what you state, it appears that all can be done within a single notebook with an iterator. We await your code. $\endgroup$ – David G. Stork Feb 1 '15 at 21:11
  • 2
    $\begingroup$ I removed the parallel tag and changed the title (your goal does not seem to involve any parallel computing). In any case, you should look into: Creating Mathematica packages which will save you a lot of hassle in the long run. $\endgroup$ – Yves Klett Feb 2 '15 at 10:31
  • 2
    $\begingroup$ The answer is use NotebookEvaluate per the linked question. The behavior of @Hogy88's (reasonable looking) attempt is interesting however. It blasts through the loop first, essentially queuing up the requested evaluation(s) to perform afterwards. (hence the appearance of some sort of parallel behavior) $\endgroup$ – george2079 Feb 2 '15 at 16:12
  • 1
    $\begingroup$ I'm voting to close this question as off-topic because it seems to have been abandoned. $\endgroup$ – Yves Klett Jul 11 '16 at 11:48
2
$\begingroup$

Per george2079's recommendation and Yves Klett's reference to How to build and operate a master Notebook?, I think this is better closed as a duplicate or not at all, since it is a slight extension of the linked Q&A. There may be times when iterating a notebook is useful. At this time, it's hard for me to imagine how the computation wouldn't be better accomplished by a package, as Yves recommended in another comment, or sometimes just by a function. Nonetheless, here is an example:

Suppose this notebook is saved in "Sample.nb":

sampleNB = 
 Notebook[{Cell[
    BoxData[RowBox[{"res", " ", "=", " ", 
       RowBox[{"{", 
         RowBox[{"res", ",", 
           RowBox[{"f", "[", RowBox[{"i", ",", "j"}], "]"}]}], 
         "}"}]}]], "Input"]}]

Suppose further it is opened with nb = NotebookOpen["Sample.nb"]; then the result will be equivalent to

nb = NotebookPut[sampleNB];

Then we can iterate evaluation of the notebook with the following:

Block[{res = {}},
 Do[NotebookEvaluate[nbobj], {i, 3}, {j, 2}];
 Flatten@res] (* Caveat: Flatten won't work right if the value of `f` has the head List *)

(*  {f[1, 1], f[1, 2], f[2, 1], f[2, 2], f[3, 1], f[3, 2]}  *)
$\endgroup$

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