Skip to main content
appended answer 86814 as supplemental
Source Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k

lets take this small example code:

Manipulate[Plot[Sin[x (1 + a* x)], {x, 0, 6}], {a, 0, 100}, Button["a=90", ToExpression["a=90"]]] 

By clicking the button I want to set the manipulate variable a to 90. An important thing is, that the variable name comes out of an imported sting in the real application. It seems, that ToExpression works for a global variable but not in the manipulate function.

How could I get to the desired functionality?

Regards!


I think I should explain my target more detailed. I have a manipulate output with approx. 20 manipulators. Furthermore I have got two buttons in the manipulate output. One button is for exporting the names and values of the manipulators in an excel spreadsheet. First column names, second column values. My intention is to save different configurations of the manipulated variables. The second button is for importing these variables. To avoid mistakes in defining the correct order of variable import (exported and imported order of variables have to match) I want to get the variable name out of the first column. So the order of import would not be relevant any more. Furthermore the code would be very compact by using a loop for importing all the variables. With the following command I have to take care about the correct order, because of the first argument of the Setter command. Dynamic@Setter[Dynamic@a, ToExpression["a=90"], "a=90"]

Here is my (not working) Import Button:

Button["Import", ImportData = Import[SystemDialogInput["FileOpen", ".xlsx"]]; For[i = 1, i < Length[ImportData[[1]]], i++, ToExpression[ ImportData[[1]][[i]][[1]] <> "=" <> ToString[ImportData[[1]][[i]][[2]]]]]] 

SetAttributes[fullName, HoldFirst]; fullName[anyVariable_, stringName_] := Module[{varFullName}, varFullName = StringReplace[ SymbolName[ Unevaluated[anyVariable]], __ ~~ scope : ("$$" ~~ DigitCharacter .. ~~ EndOfString) :> "FE`" <> stringName <> scope] ] Manipulate[Plot[Sin[x (1 + a*x)], {x, 0, 6}], {a, 0, 100}, {b, 0, 10}, Button["a", ToExpression[fullName[b, "a"] <> "=90"] ] ] 

This seems to work but I don't understand all the special characters... May you explain the fullname function? LocalizeVariables -> False does not work for me.

lets take this small example code:

Manipulate[Plot[Sin[x (1 + a* x)], {x, 0, 6}], {a, 0, 100}, Button["a=90", ToExpression["a=90"]]] 

By clicking the button I want to set the manipulate variable a to 90. An important thing is, that the variable name comes out of an imported sting in the real application. It seems, that ToExpression works for a global variable but not in the manipulate function.

How could I get to the desired functionality?

Regards!

lets take this small example code:

Manipulate[Plot[Sin[x (1 + a* x)], {x, 0, 6}], {a, 0, 100}, Button["a=90", ToExpression["a=90"]]] 

By clicking the button I want to set the manipulate variable a to 90. An important thing is, that the variable name comes out of an imported sting in the real application. It seems, that ToExpression works for a global variable but not in the manipulate function.

How could I get to the desired functionality?

Regards!


I think I should explain my target more detailed. I have a manipulate output with approx. 20 manipulators. Furthermore I have got two buttons in the manipulate output. One button is for exporting the names and values of the manipulators in an excel spreadsheet. First column names, second column values. My intention is to save different configurations of the manipulated variables. The second button is for importing these variables. To avoid mistakes in defining the correct order of variable import (exported and imported order of variables have to match) I want to get the variable name out of the first column. So the order of import would not be relevant any more. Furthermore the code would be very compact by using a loop for importing all the variables. With the following command I have to take care about the correct order, because of the first argument of the Setter command. Dynamic@Setter[Dynamic@a, ToExpression["a=90"], "a=90"]

Here is my (not working) Import Button:

Button["Import", ImportData = Import[SystemDialogInput["FileOpen", ".xlsx"]]; For[i = 1, i < Length[ImportData[[1]]], i++, ToExpression[ ImportData[[1]][[i]][[1]] <> "=" <> ToString[ImportData[[1]][[i]][[2]]]]]] 

SetAttributes[fullName, HoldFirst]; fullName[anyVariable_, stringName_] := Module[{varFullName}, varFullName = StringReplace[ SymbolName[ Unevaluated[anyVariable]], __ ~~ scope : ("$$" ~~ DigitCharacter .. ~~ EndOfString) :> "FE`" <> stringName <> scope] ] Manipulate[Plot[Sin[x (1 + a*x)], {x, 0, 6}], {a, 0, 100}, {b, 0, 10}, Button["a", ToExpression[fullName[b, "a"] <> "=90"] ] ] 

This seems to work but I don't understand all the special characters... May you explain the fullname function? LocalizeVariables -> False does not work for me.

Improved formatting
Source Link
user9660
user9660

lets take this small example code:

Manipulate[Plot[Sin[x (1 + a* x)], {x, 0, 6}], {a, 0, 100}, Button["a=90", ToExpression["a=90"]]]

Manipulate[Plot[Sin[x (1 + a* x)], {x, 0, 6}], {a, 0, 100}, Button["a=90", ToExpression["a=90"]]] 

By clicking the button I want to set the manipulate variable a to 90. An important thing is, that the variable name comes out of an imported sting in the real application. It seems, that ToExpression works for a global variable but not in the manipulate function.

How could I get to the desired functionality?

Regards!

lets take this small example code:

Manipulate[Plot[Sin[x (1 + a* x)], {x, 0, 6}], {a, 0, 100}, Button["a=90", ToExpression["a=90"]]]

By clicking the button I want to set the manipulate variable a to 90. An important thing is, that the variable name comes out of an imported sting in the real application. It seems, that ToExpression works for a global variable but not in the manipulate function.

How could I get to the desired functionality?

Regards!

lets take this small example code:

Manipulate[Plot[Sin[x (1 + a* x)], {x, 0, 6}], {a, 0, 100}, Button["a=90", ToExpression["a=90"]]] 

By clicking the button I want to set the manipulate variable a to 90. An important thing is, that the variable name comes out of an imported sting in the real application. It seems, that ToExpression works for a global variable but not in the manipulate function.

How could I get to the desired functionality?

Regards!

Source Link

ToExpression in Manipulate

lets take this small example code:

Manipulate[Plot[Sin[x (1 + a* x)], {x, 0, 6}], {a, 0, 100}, Button["a=90", ToExpression["a=90"]]]

By clicking the button I want to set the manipulate variable a to 90. An important thing is, that the variable name comes out of an imported sting in the real application. It seems, that ToExpression works for a global variable but not in the manipulate function.

How could I get to the desired functionality?

Regards!