8
$\begingroup$

I have a notebook which allows to set some parameters at the start, and then does computations depending on these parameters. It is a long notebook and for readability it is broken up into sections and subsections etc.

Now for some values of the parameters, certain sections of calculations are not necessary, so I want to skip these whole sections, without having to put them in one evaluation cell. Is this possible?

I tried putting this piece of code in the beginning of the section I want to skip:

 If[p>3,Goto[nextsection]];

And in the following section,

 Label[nextsection];

This is the effect I want, except that it doesn't work, because for Goto the label needs to be in the same input cell.

When I run it manually I can of course just skip it, or comment it out, but I'm running this notebook automatically (using the code in my own answer to my question here) so I want to automate this skipping as well.

$\endgroup$
6
  • 6
    $\begingroup$ Not an answer, but in the long run you might consider consolidating your code into Functions or Modules or even packages to facilitate repeated evaluation. This should also help eliminate errors caused by wrong evaluation order etc.. $\endgroup$
    – Yves Klett
    Commented Nov 29, 2015 at 13:46
  • 3
    $\begingroup$ If you mean you've put the code in different cells, then you can use functions like NotebookFind and SelectionEvaluate. But it seems less trouble to do as Yves suggests. $\endgroup$
    – Michael E2
    Commented Nov 29, 2015 at 14:38
  • $\begingroup$ Maybe this can be marked as a duplicate, what do you think? How to evaluate an input cell automatically after evaluating the previous one. The question is not the same but the answer is similar. $\endgroup$
    – Kuba
    Commented Nov 30, 2015 at 7:44
  • $\begingroup$ This is indeed close to an answer, but not quite yet. I want to be able to evaluate everything (ctrl+A and shift enter or have another notebook do this) and skip a subsection depending on some condition. Your answer to the other question doesn't seem to achieve this. If I use NotebookLocate["next"]; SelectionEvaluate[EvaluationNotebook[]]; in the beginning of the section that is to be skipped, and i mark some cell in the next section with next, the effect is something else. Everything is evaluated, and after this is done, the cells marked next are evaluated again $\endgroup$
    – Jansen
    Commented Nov 30, 2015 at 20:15
  • $\begingroup$ @Jansen What about putting a prolog cell as a first cell in those conditional sections, if the condition is not met it could drop evaluation from parent group and move to next. Is that ok? $\endgroup$
    – Kuba
    Commented Dec 1, 2015 at 7:45

2 Answers 2

4
$\begingroup$

I put conditional code in a delayed Block, and added a toggler which defines wither that block is to be run on "Evaluate Notebook" or not:

enter image description here

(the point of first two lines here is to make the notebook yield exactly same results upon multiple runs of "Evaluate Notebook")

If you'd rather use global variables in your conditional Block use:

testX := Block[{},
   x
   ];
$\endgroup$
2
$\begingroup$

I was looking with the same question. Best I've found... With the section selected you can simply use menu pull-down "Edit Un/Comment selection (Ctrl+/)". All inputs in the section will be commented out at the same time preventing their execution during the next global Ctrl+A select and process.

$\endgroup$

Your Answer

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

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