I try to create a static variable initialized by some control. Let's create PopupMenu control

SomeText = "SomeText";
PopupMenu[Dynamic[PS], {0 -> "Prefix", 1 -> "Suffix"}]

Then, on the base of the control, a variable StringForLater is initialized

StringForLater = Dynamic[If[PS == 0, StringJoin["Prefix", SomeText], StringJoin[SomeText, "Suffix"]]]

Later StringForLaterperforms as a dynamical variable, but I need another behavior: after selecting an item in PopupMenu make StringForLater static, not dynamic.


I think this is what you want. You can use the tracking function argument to Dynamic[]:

SomeText = "SomeText"; (* I avoid starting vars with caps; so someText *)
  (PS = #;
    StringForLater =   (* Ditto: stringForLater instead of StringForLater *)
     If[PS == 0, StringJoin["Prefix", SomeText], 
      StringJoin[SomeText, "Suffix"]]
    ) &],
 {0 -> "Prefix", 1 -> "Suffix"}]


(Avoiding initial capitals means my symbols won't conflict with built-in symbols.)

  • $\begingroup$ Thank you. Indeed, It works as needed. I'm surprised that Mathematica needs some tricks in order to implement simple things :(( $\endgroup$ – Konstantin Dec 1 '20 at 17:54

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.