Message Boards Message Boards


Create a loop that spans sections

Posted 9 years ago
2 Replies
1 Total Likes


I have a created a notebook that is fairly in-depth and contains many sections. The sections are there more as documentation than anything else.

At the beginning of the notebook, I import data and use that data for the rest of the notebook. At the end, the notebook generates a document with the relevant results.

What I would like to do is create a loop that spans the entire notebook (i.e. spans multiple sections) for multiple sets of data and get multiple documents. Is it possible to do this with the sections intact? If I need to remove the sections, what is a good way to leave the documentation intact?

Thank you.

2 Replies
Posted 9 years ago


Thanks for the help. The tagging suggestion seemed like a lot of work.

After messing around with the second suggestion, it worked like a charm after I realized how to pass the info to the notebook doing the work. In my case the loop looks like:

Do[(work = testList[[i]][[1]]; NotebookEvaluate["\\folder\newTest.nb"];), {i, 1, Length[files]}]

where work is the variable I used in the evaluated notebook.

Thanks again, William


I am a bit unclear about what exactly you want to do, though I am sure that something that fits what I think you are wanting can be done.

Do you want some code that runs outside of the specified notebook to do the processing or do you want the code to be within the notebook itself?

Either way, this might not be trivial (but see my final comments below where a simplified approach may be workable depending on the particular workflow you have). It generally involves using notebook programming constructs to move around in the notebook and to execute the particular cells as desired. To execute cells from code that is controlling that execution you will need the function NotebookEvaluate and one would then find particular cells in the original notebook, create a temporary notebook with those cells, and execute the temporary notebook with NotebookEvaluate. Finding those cells may involve using NotebookFind using CellTags and having the original notebook's cells properly tagged. (See for more than you may ever want to know about tagging.... ).

Another approach is to, at the end of each Input cell in your notebook, have code that uses NotebookFind and then SelectionEvaluate as the final expressions in that cell which points (via CellTags) to the next cell in the notebook (wherever it is) that should be executed.

Perhaps there are other approaches which may work, depending on the detailed particulars of what you want to do.

Finally, it may simply be that you want to (a) specify the data source that you want to access prior to each time you want to fully execute the notebook and then (b) execute the notebook. If this is the case then, if we call the notebook's NotebookObject nb(assumed for this discussion to be an open notebook), the form of the code you would create and which would be executed from a different notebook would be

 {i, 1, nIterations}]

where assignExternalDataSourceParameters[i] just represents the code that does the assignment of the needed data source locations (e.e., file names) in order for your notebook to run for those.

POSTED BY: David Reiss
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract