2

I have a very complicated analysis running on a notebook named Called_notebook.nb which depends on two parameters. To automate the process, I call this notebook in a loop from a master notebook, like this:

Monitor[

 Do[
    Do[

        linked`i = i;
        linked`j = j;
        NotebookEvaluate[NotebookDirectory[] <> "Called_notebook.nb"],      

     {j, 10}],
 {i, 27}],


 {i, j}]

The problem is that the Called_notebook.nb is supposed to export figures to .png files, which does not happen within this approach (that is, the export command works when I run the slave notebook by itself but does not - no file appears - when I run it from the master notebook). I also tried to move the Export command to the master notebook, below the NotebookEvaluate, but then I get an error within the loop.

How can I solve this?

GaloisFan
  • 405
  • 2
  • 7

2 Answers2

2

I can present a solution to this issue (as far as I understand it).

The slave.nb notebook generates images dependent on the $i$ and $j$ parameters. The master.nb notebook performs a double loop controlled by the indices $i$ and $j$ calling the slave.nb notebook. Export takes place both from slave.nb and master.np. Exported files differ in their names depending on the location of the export command. They also differ from each other with the indices $i$ and $j$ specified in the name and the form of the image depending on these parameters.

slave.nb:

dir = NotebookDirectory[];
SetDirectory[dir];
image = ImageCompose[Image[RandomReal[{0, 1}, {100*i, 100*j}]], 
  Rasterize[StringJoin["i=", ToString[i], ", j=", ToString[j]], 
   RasterSize -> 75]] (*example i and j - dependant image*)
filename = 
 StringJoin["image-from-slave-", ToString[i], "-", ToString[j], 
  ".png"]
Export[filename, image]

master.nb:

dir = NotebookDirectory[];
SetDirectory[dir];
nb = StringJoin[ToString[NotebookDirectory[]], "slave.nb"];
For[i = 1, i <= 3, i++,
  For[j = 1, j <= 5, j++,
    NotebookEvaluate[nb];
    filename2 = 
     StringJoin["image-from-MASTER-", ToString[i], "-", ToString[j], 
      ".png"];
    Export[filename2, image];
    ];
  ];

Just SAVE these files in the COMMON directory (preferably in an empty folder) and run master.nb.

It turns out that the export runs correctly from both notebooks.

enter image description here

Druid
  • 395
  • 1
  • 11
  • Hi! Thanks for the answer. What you propose is actually exactly my attempt, which does not work. – GaloisFan May 02 '19 at 20:04
  • @GaloisFan But it does work! :) Please take the notebooks that I provided above and try. You can also compare your result with my - visible on the screenshot above. – Druid May 02 '19 at 20:35
  • So, the only difference between our codes is that you use a for loop while I make use of the Do syntax. Perhaps that is what this is about. Upvoted! – GaloisFan May 03 '19 at 12:22
  • @GaloisFan It seemed unlikely. And indeed, after rewriting the code using the loops Do[Do[_,{j,5}],{i,3}] everything still works correctly - export occurs from both notebooks. The difference certainly lies elsewhere. Please check yourself - here is an alternative version of the notebook master.nb: link – Druid May 03 '19 at 12:46
1

Use UsingFrontEnd e.g.

UsingFrontEnd[NotebookEvaluate[NotebookDirectory[] <> "Called_notebook.nb"]]

and possibly

UsingFrontEnd[Do ...
  NotebookEvaluate[NotebookDirectory[] <> "Called_notebook.nb"]]
 ...]

also mentioned here

Chris Degnen
  • 30,927
  • 2
  • 54
  • 108