# Changing the value of a variable based on output of manipulate

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.

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.

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.

• That worked like a charm. Thanks! Sep 8 '19 at 0:23