Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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
share|improve this question
    
you have commented about a further below but you should fix up this question describing what a is and how it relates to char. –  Mike Honeychurch Dec 5 '12 at 20:44
add comment

4 Answers 4

up vote 3 down vote accepted

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.

share|improve this answer
    
Very slick. :-) –  Mr.Wizard Dec 6 '12 at 11:31
add comment

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).

share|improve this answer
    
No I want it to change a value generated by FromCharacterCode and in my example it should modify a. –  Tyilo Dec 5 '12 at 19:24
    
But your example code does not contain any variable a! What is the variable a, and what is it supposed to be changed to? –  Jens Dec 5 '12 at 19:26
    
ToExpression@FromCharacterCode@97 == a –  Tyilo Dec 5 '12 at 19:26
add comment

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}]
]
share|improve this answer
    
You might want to use ToExpression[FromCharacterCode@char, InputForm, Unevaluated] in case a already has a value. –  Simon Woods Dec 6 '12 at 10:11
    
@SimonWoods Good point! –  Sjoerd C. de Vries Dec 6 '12 at 10:40
add comment

Here's another formulation:

Slider[Dynamic @@ ToHeldExpression @ FromCharacterCode @ char, {0, 5}]
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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