2
$\begingroup$

I'm trying to save some settings and results from Manipulate, and I want to be able to save them to different variable names. The code I have is something like

Manipulate[x,
 {{x, x1, "value"}, ControlType -> InputField},
 {{variablename, defaultname, "variable name"}, ControlType -> InputField},
 Button["save", variablename = x]]

Mathematica graphics

So, the functionality I want is that I can write a variable name into the second InputField, hit save, and have the value of x assigned to a variable of that name.

However as it stands, hitting save only updates the value of the variablename variable within Manipulate.

Any help appreciated.

$\endgroup$
  • $\begingroup$ You don't want to actually write to a named file on disk. You just want to assign a value to a variable whose name is a variable itself. Is this interpretation of your question correct? $\endgroup$ – Sjoerd C. de Vries Nov 11 '15 at 13:22
  • $\begingroup$ yes! that's it. $\endgroup$ – Dan Goldwater Nov 11 '15 at 13:40
  • $\begingroup$ question now updated to reflect this. $\endgroup$ – Dan Goldwater Nov 11 '15 at 13:48
1
$\begingroup$

This will work so long as you don't try to assign a variable that already has an assignment

Manipulate[x, 
  {{variablename, defaultname, "variable name"}, ControlType -> InputField}, 
  {{x, x1, "value"}, ControlType -> InputField}, 
  Button["save", Evaluate[variablename] = x]]
$\endgroup$
  • 2
    $\begingroup$ To remove that constraint is pretty difficult, by the way. $\endgroup$ – Sjoerd C. de Vries Nov 11 '15 at 14:28
1
$\begingroup$

Provide name as a String and use one of those methods to assign the value: How do you programatically load data into symbols?

Manipulate[x,
 {{variablename, "test", "variable name"}, (InputField[#, String] &)}, 
 {{x, x1, "value"}, ControlType -> InputField}, 
 Button["save", 
  ToExpression[variablename, StandardForm, Function[name, name = x, HoldFirst]
  ]
 ]
]
$\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.