6
$\begingroup$

I´m not quite sure if I ask a stupid question but I could not find an appropriate answer in the forum...

I have a variety of cells (= code segments) in my notebook which I would like to evaluate in certain combinations. Currently I use a variables based approach to "disable" the cells I do not want to be evaluated. e.g.

evaluateRoutine1 = True
evaluateRoutine2 = False
evaluateRoutine3 = True
...

If[evaluateRoutine1 = True,

function1;
function2;

]

I know I could use the comment function (* *) as well but it is a little bit awkward to go through the code all the time to disable cells ...

Is there a better possibility to prevent cells to be evaluated?

Thanks

$\endgroup$
  • 1
    $\begingroup$ Look at the Evaluatable in documentation Options for Cells $\endgroup$ – Jens Mar 20 '15 at 17:37
4
$\begingroup$

In addition to Jens comment:

This is how you can find the cellproperties:

cell properties

EDIT(1):

I've got an idea on how to improve the variable based method. You could create a wrapper function call it: code wich holds additional code, that can be turned on/off like this:

evaluationRoutines = {1 -> True, 2 -> False, 3 -> True};

ClearAll[code]
SetAttributes[code, HoldAll]

code[args__][i_Integer] := 
If[i /. evaluationRoutines, ReleaseHold[args]]

With this you write the code separated by ; into the first bracketslot of code[][]. You define the number of the evaluation rutine within the second bracketslot. This might look a little awkward... Why not using the first slot for the number instead? Because the Attribute HoldAll will only work for the first brackets, e.g. for args. The use of the attribute HoldAll is important to keep the arguments unevaluated until we want to evaluate them.

This is an example of how you would use it:

In:

Clear[i]

code[

   i = 1;
   i++;
   Print[i]

][2]

Out:

no Output was generated, because the value 2 is replaced by False as defined in evaluationRoutines and If evaluates to Null. You can see that the code has not been evaluated by the colloring of i. It is still blue so it has no associated value.

Clear[i]

code[

   i = 1;
   i++;
   Print[i]

][1]

Out:

2

Now the block of code was evaluated and i now has the value 2

EDIT(2):

If you dont like to give evaluationRoutines as rules use this one:

evaluationRoutines = {True, False, True};

ClearAll[code]
SetAttributes[code, HoldAll]

code[args__][i_Integer] := 
If[evaluationRoutines〚i〛, ReleaseHold[args]]
$\endgroup$
  • $\begingroup$ Thanks, I used the cell properties already but I have about 40 code segments I combine in different ways. In so far I think I move on with my variables based approach as it might be a little bit too awkward to scroll through all the code to change the cell properties all the time. Nevertheless thanks for your help. $\endgroup$ – Phadreus Mar 22 '15 at 16:56
  • $\begingroup$ @Phadreus See my edit for potential improvement of your variable based method. $\endgroup$ – sacratus Mar 22 '15 at 17:56
1
$\begingroup$

I know this is from forever ago, but I ran into this recently for my own work now.

Luckily there's now Cell Tags (Command-J on Mac, under Cell->Cell Tags->, or via right-clicking on a cell) to label cells with tags (many-to-many).

This lets you select a bunch of specific cells at once, with Cell->Cell Tags->Find Tag->[tag].

You can thus more easily toggle all cells with a certain tag from evaluating or not. If you don't care about the appearance, you can just use the styling keyboard shortcuts to convert your selected cells to a (non-evaluatable) Text cell (command-7) or an input cell (command-9).

$\endgroup$

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.