4
$\begingroup$

If I execute the following code:

char = 97;
Slider[Dynamic@ToExpression@FromCharacterCode@char, {0, 5}]

the slider will fail to change the value of a.

How do you make this work, when the expression has to be generated with FromCharacterCode or similar?

Note:

ToExpression@FromCharacterCode@97 == a
$\endgroup$
1
  • $\begingroup$ you have commented about a further below but you should fix up this question describing what a is and how it relates to char. $\endgroup$ Commented Dec 5, 2012 at 20:44

4 Answers 4

3
$\begingroup$

Perhaps the simplest thing would be to put the string "Dynamic@" inside ToExpression:

Slider[ToExpression["Dynamic@" <> FromCharacterCode@char], {0, 5}]

This creates a slider which controls the value of the symbol a, which I think is what you are asking for.

$\endgroup$
1
  • $\begingroup$ Very slick. :-) $\endgroup$
    – Mr.Wizard
    Commented Dec 6, 2012 at 11:31
4
$\begingroup$

How about using With?

char = 97;
With[{var = ToExpression@FromCharacterCode@char},
  Slider[Dynamic@var, {0, 5}]
]

or, if a already exists, following Simon Woods' suggestion:

char = 97;
With[{var = ToExpression[FromCharacterCode@char, InputForm, Unevaluated]},
  Slider[Dynamic@var, {0, 5}]
]
$\endgroup$
2
  • $\begingroup$ You might want to use ToExpression[FromCharacterCode@char, InputForm, Unevaluated] in case a already has a value. $\endgroup$ Commented Dec 6, 2012 at 10:11
  • $\begingroup$ @SimonWoods Good point! $\endgroup$ Commented Dec 6, 2012 at 10:40
3
$\begingroup$

I think you mean something like this:

Clear[char];
{Slider[Dynamic[char], {67, 97, 1}], Dynamic[FromCharacterCode[char]]}

The syntax in your question was incorrect because the dynamic variable in the first argument of Slider can't be a protected function name.

Edit

You also had a ToExpression in your question. That can be added in this answer too, obviously by writing ToExpression@FromCharacterCode. Then you just have to make sure your character range doesn't include characters that yield an error when fed into ToExpression.

Edit 2 In response to the comment, I think what you should do is to write

a = Dynamic[ToExpression@FromCharacterCode[char]]

with the above slider (or some version of it).

$\endgroup$
3
  • $\begingroup$ No I want it to change a value generated by FromCharacterCode and in my example it should modify a. $\endgroup$
    – Tyilo
    Commented Dec 5, 2012 at 19:24
  • $\begingroup$ But your example code does not contain any variable a! What is the variable a, and what is it supposed to be changed to? $\endgroup$
    – Jens
    Commented Dec 5, 2012 at 19:26
  • $\begingroup$ ToExpression@FromCharacterCode@97 == a $\endgroup$
    – Tyilo
    Commented Dec 5, 2012 at 19:26
1
$\begingroup$

Here's another formulation:

Slider[Dynamic @@ ToHeldExpression @ FromCharacterCode @ char, {0, 5}]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.