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.

I'm trying to do the following:

I have a table:

w = Table[ToExpression[ToString[h] <> ToString[i]], {i, 1, Length[c]}]

This table is suposed to generate variables, like the following list:

{h1, h2, h3, h4, h5, h6, h7, h8, h9, h10, h11}

I'm trying to create a table of vertical sliders with this by adding this to the code:

w = Table[VerticalSlider[Dynamic[ToExpression[ToString[h] <> ToString[i]]]], {i, 1, Length[c]}]

It create a it gives me an error:

Set::write: "Tag ToExpression in \!\(ToExpression[\"hi\"]\) is Protected.

I'm aware that this may not be the best way todo this but it's all I can imagine, I guess the other feasible way would be with loops, I don't know. Can you help me?

share|improve this question
2  
Your code doesn't work as it is because c is not defined anywhere. –  gpap Mar 7 '13 at 15:59
    
Related: mathematica.stackexchange.com/q/16893/5 –  rm -rf Mar 7 '13 at 18:25
    
@gpap It is defined in the code, but I copied only what is relevant to the question. –  Vladimir Putin Mar 7 '13 at 19:39
1  
I'm sure @gpap knew that. He was probably just pointing out that you posted a non working snippet. Which is easily fixed, but you might have taken the time to do that your self. –  Ajasja Mar 7 '13 at 19:51
    
Yes, @ajasja is right: I just thought that it's better to have working code for people to copy-paste and experiment with. I think you should edit it appropriately either replacing Length[c] with whatever length c has in your original code or give c (not the case here, but it could have been that c was too long and that caused your problem - a working snippet would help exclude such cases). –  gpap Mar 7 '13 at 21:19

2 Answers 2

The problem is that Dynamic is preventing ToExpression from evaluating to a symbol. When you drag the slider it's trying to evaluate:

ToExpression[ToString[h] <> ToString[1] = 0.1

and you get a message about not being able to set the value of ToExpression.

I would use With to create the symbols and insert them into the Dynamic:

w = Table[With[{var = ToExpression[ToString[h] <> ToString[i]]}, 
       VerticalSlider[Dynamic[var]]], {i, 1, Length[c]}]
share|improve this answer

Depending on what you're going to do next, this might be useful:

 x = {.1, .2, .3, .4, .5};
 Outer[Slider[Dynamic[x[[#1]]], {0, 1}] &, Range[Length[x]]]

This creates a collection of sliders, one for each element of x. The values of x provide the initialization, and when you move a slider, the corresponding element of x changes. You can see this happening in real time using

 Dynamic[x]
share|improve this answer
1  
+1 Btw Slider[Dynamic[x[[#1]]], {0, 1}] & /@ Range@Length@x is another option (I'm just not used to seeing Outer instead of Map ;) –  Ajasja Mar 7 '13 at 18:59
    
You're right... I might have written it in functional form Map[Slider[Dynamic[x[[#1]]], {0, 1}] &, Range[Length[x]]] rather than postfix form, but Map[ ] is probably clearer than Outer[ ]. –  bill s Mar 7 '13 at 19:30

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.