How to run a whole mathematica notebook within a for loop?

Yep.

You can do this no problem. I suggest looking at the documentation on the workflow in NotebookEvaluate[]. For example, create another notebook with the following contents.

Table[ NotebookEvaluate["/path/to/previous/notebook.nb"], {k, k_min, k_max}]

I'd suggest that maybe a simpler way would be to confine operations to a single note book with the following structure

  • Initialise
  • Loop: read, then process, the plot each data file
  • Wrap up

Easier debugging for one, but your kilometerage may vary.


While it is possible to use NotebookEvaluate for such purposes, I think it will be worth to learn the more recommended ways to automate your tasks:

  1. you can write scripts in text-only files which can end in either .m or .wl and run these either from the command line or by simply doing Get["/path/to/script"].

  2. Even better is to learn how to write functions, then call the functions in a loop. This would look something like:

    exportPlot[n_]:=Module[{importfilename,exportfilename},
       index = ToString[n];       
       importfilename = StringJoin["/path/to/data/data", index, ".dat"];
       exportfilename = StringJoin["/path/to/graphs/graph", index, ".pdf"];
       (** Lines to generate the plot from importfilename **)
       Export[exportfilename, %]
    ];
    
    Do[exportPlot[k],{k,1,10}]
    

Another note: I would strongly suggest to avoid to use % within such code/scripts. It doesn't cost you anything to assign any result to a variable and use that variable in the following code. It will make your code much easier to read and a lot less error prone.

Tags:

Import

Export