2
$\begingroup$

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?

$\endgroup$
1
$\begingroup$

Use UsingFrontEnd e.g.

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

and possibly

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

also mentioned here

$\endgroup$
  • $\begingroup$ I'm not sure how to use this. It doesn't look like a command in Mathematica 10. Do I simply put it in the code as you wrote? Thank you! $\endgroup$ – GaloisFan May 2 at 16:25
  • $\begingroup$ Beware that are still lots of bugs in UsingFrontEnd $\endgroup$ – user5601 May 2 at 16:44
  • $\begingroup$ So, I just added UseFrontEnd as you did, but it did not work. $\endgroup$ – GaloisFan May 2 at 17:01
  • $\begingroup$ @user5601 Would you advice its use like this? The solution proposed in the answer did not work. $\endgroup$ – GaloisFan May 2 at 17:02
  • $\begingroup$ Did you mean to use UsingFrontEnd? I think UseFrontEnd is a JLink symbol, not sure what it does. $\endgroup$ – Jason B. May 2 at 19:17
2
$\begingroup$

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

$\endgroup$
  • $\begingroup$ Hi! Thanks for the answer. What you propose is actually exactly my attempt, which does not work. $\endgroup$ – GaloisFan May 2 at 20:04
  • $\begingroup$ @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. $\endgroup$ – Druid May 2 at 20:35
  • $\begingroup$ 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! $\endgroup$ – GaloisFan May 3 at 12:22
  • $\begingroup$ @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 $\endgroup$ – Druid May 3 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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