7
$\begingroup$

Is it possible to evaluate only the selected cells programmatically that I am interested in? Say in current cell to bottom of the notebook, instead of evaluate notebook or cells?

$\endgroup$
6
  • $\begingroup$ Can you make more clear what you mean? Evaluate selected cells that I'm interested in..? Which one do you want to evaluate? The selected or the interesting? I don't understand the last sentences. Please describe clearly and detailed what you have (make a small example!) and what you try to achieve. $\endgroup$
    – halirutan
    Nov 19, 2012 at 1:01
  • $\begingroup$ Say I have cells 1 to 10 in total, and I simply want to evaluate from cell 5 onward to the rest of the notebook. $\endgroup$ Nov 19, 2012 at 1:20
  • 4
    $\begingroup$ You've asked a few questions in the past couple of days, all about programmatically and repeatedly evaluating notebooks/cells/etc, which seem to be a very convoluted way of doing things. My guess is that you want to control multiple runs of simulations (the commands for which, are in certain select cells) and this is your way of doing it. If so, I would strongly urge you to encapsulate your code/simulations as a function and do multiple runs of that using Do or Table, instead of code-gymnastics to control cell evaluation. You might also want to look into putting your stuff in a package. $\endgroup$
    – rm -rf
    Nov 19, 2012 at 1:43
  • 3
    $\begingroup$ Sebastian: many people have suggested to you to avoid doing Notebook manipulation. Please follow their advice, cell manipulation is useful in certain cases but it is not a way to structure execution. Unless, of course, you explicitly want to do Notebook manipulation to control export, rendering, etc. But honestly your questions don't seem to point in that direction... help us help you, what are you really doing? $\endgroup$
    – carlosayam
    Nov 19, 2012 at 10:12
  • $\begingroup$ @caya: This is a very valid question and suggestion. I find it extremely useful to evaluate a notebook to or from the cell insertion. See my answer below for simple implementation. $\endgroup$ Nov 19, 2012 at 14:28

2 Answers 2

9
$\begingroup$

Two buttons of my ButtonTools palette (originally written for Mathematica 4, published here: http://www.mertig.com/mathdepot/ ) will do what you want.

I refactored the code here. So just evaluate the code below. Then select the notebook you want to partially evaluate. One button will evaluate all cells from the top until the insertion point, the other all cells downward.

I use it all the time since often I do not want to reevaluate the whole notebook and it is much quicker to press a button than to select a bunch of cells.

Also, this is of course useful for development/debugging/documentation. The code itself should be as much functional as possible, obviously.

CreatePalette[{Button[Style["E\[UpArrow]", "Section"], 
   SelectionMove[SelectedNotebook[], After, Cell];
   NotebookWrite[SelectedNotebook[], 
   Cell["bishierherundnichtweiter", "Input", CellOpen -> False, 
   ShowCellBracket -> False, 
       CellElementSpacings -> {"CellMinHeight" -> 0}, 
   CellMargins -> {{0, 0}, {0, 0}}]];
   SelectionMove[SelectedNotebook[], Before, Notebook];
   Catch[
   Do[SelectionMove[SelectedNotebook[], Next, Cell];
      Module[ {r},
          If[ MatchQ[r = NotebookRead[SelectedNotebook[]], 
            Cell["bishierherundnichtweiter", ___]],
              NotebookDelete[SelectedNotebook[]];
              Throw[0],
              SelectionEvaluate[SelectedNotebook[]]
          ]
      ], {100000}]]], 
     Button[Style["E\[DownArrow]", "Section"], 
   SelectionMove[SelectedNotebook[], Before, Cell];
   NotebookWrite[SelectedNotebook[], 
   Cell["Start Evaluation here", "SmallText", CellOpen -> True, 
   CellMargins -> {{0, 0}, {0, 0}}, 
       CellTags -> "hiergehtslos"]];
   SelectionMove[SelectedNotebook[], After, Notebook];
   NotebookWrite[SelectedNotebook[], 

    Cell["Stop Evaluation here", "SmallText", CellOpen -> True, 
     ShowCellBracket -> True, CellMargins -> {{0, 0}, {0, 0}}, 
            CellTags -> "undhieristschluss"]];
   NotebookFind[SelectedNotebook[], "hiergehtslos", All, CellTags];
   Catch[
   Do[SelectionMove[SelectedNotebook[], Next, Cell];
      Module[ {r},
          If[ MatchQ[r = NotebookRead[SelectedNotebook[]], 
            Cell["Stop Evaluation here", "SmallText", ___]],
              Throw[0],
              SelectionEvaluate[SelectedNotebook[]]
          ]
      ], {10000}]];
   NotebookFind[SelectedNotebook[], "hiergehtslos", All, 
   CellTags, AutoScroll -> False];
   NotebookFind[SelectedNotebook[], "hiergehtslos", All, CellTags, 
          AutoScroll -> False];
   NotebookDelete[SelectedNotebook[]]*
    NotebookFind[SelectedNotebook[], "undhieristschluss", All, 
     CellTags];
   NotebookFind[SelectedNotebook[], "undhieristschluss", All, 
   CellTags];
   NotebookDelete[SelectedNotebook[]]]}]
$\endgroup$
0
$\begingroup$

Yes. Just select the cells you want to evaluate one by one (keeping Ctrl pressed) and then press Shift+Enter.

$\endgroup$
1
  • 4
    $\begingroup$ I believe the OP is seeking a programmatic solution, not a manual one. $\endgroup$
    – Michael E2
    Aug 18, 2013 at 18:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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