Re: Running Notebooks inside a notebook:

*To*: mathgroup at smc.vnet.net*Subject*: [mg91115] Re: Running Notebooks inside a notebook:*From*: David Bailey <dave at Remove_Thisdbailey.co.uk>*Date*: Wed, 6 Aug 2008 05:08:02 -0400 (EDT)*References*: <g791fg$9k2$1@smc.vnet.net>

Gopinath Venkatesan wrote: > Hi Mathematica Friends, > > I wanted to execute a notebook inside a notebook. Say I have a notebook "Outer.nb" and "Inner.nb". I tried to do it using NotebookOpen[], Please see the code in the bottom for the same. > > Inner.nb has a list of data input and commands and then outputs. Now I want to put this Inner.nb inside a Do[] loop in the Outer.nb, such that for each loop increment, I could change the input of the Inner.nb, and as well as write the output to a separate file (say Excel), or variable. Here it opens the notebook but doesn't do any calculations. > > One thing to note here is the parameters vel,nmx,n are all needed for successful execution of Inner.nb but I hope that because the kernel remembers these values from Outer.nb, I did not include them in the Inner.nb. Moreover, I wanted to access these from Outer.nb because I wanted to change these values in the loop. > > ******************************* > (* Code starts here -- This is Outer.nb file *) > vel = 20; > nmx = 3; > n = 31; > set = {}; > While[0 <= vel <= 40, > file = "G:/somefolder/Inner.nb"; > nb = NotebookOpen[file, Visible -> True]; (* True/False visible/hidden does not matter for me *) > SelectionMove[nb, All, Notebook]; > SelectionEvaluate[nb]; > set = Append[set, newc]; > vel = 1.5 vel; > NotebookClose[nb]]; > set // MatrixForm > > (* Code ends here *) > > If you have an example or pointer to the documentation, please share it here. Thank you. > > Regards, > > Gopinath > Forget about NotebookOpen, SelectionEvaluate, and related functions. These are for more specialised applications, such as writing palettes that perform various notebook manipulations. Your notebook inner.nb should consist of one or more function definitions - say just myInnerNotebook[] - containing the code that you want to execute repeatedly. Once you have executed this definition, you can call it as required from the code in the outer notebook. However, you need to be very careful that you do not inadvertently re-use a variable in the two notebooks. Ideally your functions will be defined using Module, and most of the variables will be local to avoid unwanted interactions. Most code developers embed almost all their code inside functions. Writing a set of raw Mathematica statements, as you have done, works well to begin with, but as you have found, this technique rapidly becomes very clumsy. David Bailey http://www.dbaileyconsultancy.co.uk