0
$\begingroup$

I have an input cell wich I need to evaluate several times with distinct set of parameters. For sure, it is possible to convert the cell into a Module and run it in a cycle from another cell. Just for fun, I tried to find some other solution. Here is MWE example. It consists of two cells.

Clear[sets]
sets = 2;
Print["*** sets=", sets]
nb = EvaluationNotebook[]
runCell = NotebookLocate["RunCell2"]
FrontEndTokenExecute["EvaluateCells"]

sets = 4;
Print["*** sets=", sets]
(*nb=EvaluationNotebook[]*)
runCell = NotebookLocate["RunCell2"]
FrontEndTokenExecute["EvaluateCells"]

Main cell:

(* Run Cell *)
Clear[ab];
nb = EvaluationNotebook[]; cell = 
 EvaluationCell[]; cellTagOld =.; cellTag =.;
SetOptions[cell, CellTags -> {"RunCell"}, ShowCellTags -> True];
Print["sets=", sets];

Do[
 cellTag = ToString@SessionTime[];
 CellPrint[
  TextCell[
   ToString@
    StringForm["Do cycle ``", ToString[i] <> "/" <> ToString[sets]], 
   "Subsection", CellTags -> {cellTag}, ShowCellTags -> True, 
   CellDingbat -> "\[SmallCircle]"]];
 Do[Print[ab[i, j]], {j, 4}];
 
 NotebookLocate[cellTagOld];
 Pause[5];
 FrontEndTokenExecute["SelectionCloseAllGroups"];
 cellTagOld = cellTag;
 Pause[1]
 , {i, sets}]

First cell tries to evaluate the second cell 2 times with parameter sets equal to 2 and 4. However, in fact the RunCell is evaluated twice with sets=4. Evidently, this is because Mathematica does not wait for the first evaluation FrontEndTokenExecute["EvaluateCells"] to finish and evaluates the first cell to the end. Can it be corrected somehow?

$\endgroup$

0

Your Answer

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

Browse other questions tagged or ask your own question.