0
$\begingroup$

Is there a user-friendly way to get a Mathematica notebook to choose between If statements according to the value of a variable?

I would like the user to be able to choose one of several conditions and also (perhaps somewhat later) to be able to write their own condition if they wish.

At the moment a chunk of the code looks like this:

(* If [condition 1] *) (* Description of what this does *)
(* If [condition 2] *) (* Description of what this does *)
(* If [condition 3] *) (* Description of what this does *)
(* If [condition 4] *) (* Description of what this does *)
...

and the user chooses which condition they wish to apply and removes its commenting out, leaving all the other conditions commented out and therefore not applied.

This would be easier if I could describe in a comment what each condition is for and then ask the user to set a single variable, like this:

(* Condition 1 does [description] 
   Condition 2 does [description] 
   Condition 3 does [description] 
   Condition 4 does [description] *)
Please set the variable COND to determine which condition is used. *)
COND=2 ;

Then all the user has to do is change that 2 to the number for whatever condition they want to use. They don't have to delete or add any asterisks or parentheses.

The question is how can I make the program choose among the If statements according to the value of COND? I need the If statements to be described as neatly as they are in the existing code, so that the user can add a new one in later. Surrounding them with too many parentheses and commas could cause confusion. Then if the user writes a new If statement they should be able to assign it its own number. For example here they would call it condition 5.

$\endgroup$
2
  • 3
    $\begingroup$ Maybe you should consider using a combination of Manipulate and a template notebook. $\endgroup$ – Anton Antonov Nov 23 '20 at 15:50
  • 4
    $\begingroup$ As @AntonAntonov is suggesting, I think you're trying to find a solution for a problem other than the one you have. Your issue is that you want a user to be able to supply different behavior & have that be named easily. The obvious solution to this is that your provide them a set of build block functions and then they can make use of what you've done in their own function. Or if they just need to do one small bit of data pre/post-processing you can use the functional pattern myProgram[userDataFunc_, other inputs ...]:=...; This will be less work long term and way more powerful/flexible $\endgroup$ – b3m2a1 Nov 23 '20 at 16:31
3
$\begingroup$
Manipulate[
 Column[{
   "If Condition = 1 Then... Description of what this does",
   "If Condition = 2 Then... Description of what this does",
   "If Condition = 1 Then... Description of what this does",
   "",
   Which[
    Condition == 1, "Condition 1 output",
    Condition == 2, "Condition 2 output",
    Condition == 3, "Condition 3 output"
    ]
   }],
 {Condition, {1, 2, 3}},
 ControlType -> RadioButton
 ]

enter image description here

You can use this template to represent any kind of output. You can also use a number of different ControlTypes (see documentation).

I generally find using Which[ ] clearer to read than If[ ] statements, especially if you have multiple possible selections. This of course makes extending the code easier for you or another user.

If you really want to enable an end user to easily add new conditions and output, that requires a bit more more work. Certainly possible, but this needs a bit of meta level design.

@b3m2a1's comment has already suggested a direction for this.

You may want to recast your question if you really want this kind of meta level functionality.

$\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.