Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
    
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. –  halirutan Nov 19 '12 at 1:01
    
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. –  sebastian c. Nov 19 '12 at 1:20
4  
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. –  rm -rf Nov 19 '12 at 1:43
3  
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? –  caya Nov 19 '12 at 10:12
    
@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. –  Rolf Mertig Nov 19 '12 at 14:28
show 1 more comment

2 Answers

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[]]]}]
share|improve this answer
add comment

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

share|improve this answer
4  
I believe the OP is seeking a programmatic solution, not a manual one. –  Michael E2 Aug 18 '13 at 18:07
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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