0
$\begingroup$

I'm trying to use the output value of a manipulate to define a variable that will be used for a calculation. I've created a minimal example of what I'm trying to do. In this case, I'd like to use the output value of test to define the variable testVar so that I can use testVar in defining testCal.

    Manipulate[
 testVar = If[test == "b", 2 , 5];
 testCalc = testVar*2;
 Text[testCalc],
 {{test, "a", "Test Manipulate"}, {Style["a", 14, Red], 
   Style["b", 14, Blue], Style["c", 14, Green]}}
 ]

When I run this code, I get the correct output, which seems promising.

enter image description here

Unfortunately, when I click on any of the options, testCalc doesn't properly evaluate. I've tried using Dynamic in a few places, but haven't been able to figure out how to get testCalc to properly evaluate when I click on the different options for Test Manipulate.

enter image description here

$\endgroup$
0
$\begingroup$

Try

Manipulate[
 testVar = If[SameQ[test, Style["b", 14, Blue]], 2, 5];
 testCalc = testVar*2;
 Text[testCalc],

 (*controls below*)

 {{test, "a", "Test Manipulate"}, {Style["a", 14, Red], 
   Style["b", 14, Blue], Style["c", 14, Green]}},

 TrackedSymbols :> {test}
 ]

First, your test is not string, it is Style["b", 14, Blue], second use SameQ[] and not ==. Also notice that your control variable test is not String. it is actually a Style. So you can not compare String with a Style.

When you do "a" == Head[Style["a", 14, Red]] Mathematica does not return True nor False. It returns the same expression. That is why you were hitting the 3rd argument of If which you did not have.

enter image description here

$\endgroup$
  • $\begingroup$ That worked like a charm. Thanks! $\endgroup$ – Doug S Sep 8 at 0:23

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.