2
$\begingroup$

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?

$\endgroup$
5
  • 2
    $\begingroup$ Your code doesn't work as it is because c is not defined anywhere. $\endgroup$
    – gpap
    Commented Mar 7, 2013 at 15:59
  • $\begingroup$ Related: mathematica.stackexchange.com/q/16893/5 $\endgroup$
    – rm -rf
    Commented Mar 7, 2013 at 18:25
  • $\begingroup$ @gpap It is defined in the code, but I copied only what is relevant to the question. $\endgroup$
    – Red Banana
    Commented Mar 7, 2013 at 19:39
  • 1
    $\begingroup$ 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. $\endgroup$
    – Ajasja
    Commented Mar 7, 2013 at 19:51
  • $\begingroup$ 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). $\endgroup$
    – gpap
    Commented Mar 7, 2013 at 21:19

2 Answers 2

4
$\begingroup$

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]}]
$\endgroup$
3
$\begingroup$

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]
$\endgroup$
2
  • 1
    $\begingroup$ +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 ;) $\endgroup$
    – Ajasja
    Commented Mar 7, 2013 at 18:59
  • $\begingroup$ 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[ ]. $\endgroup$
    – bill s
    Commented Mar 7, 2013 at 19:30

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.