My code looks like this:

Column[{InputField[Dynamic[x], String, ContinuousAction -> True], ToExpression[Dynamic[x]], Dynamic[Head[x]]}]

When I intentionally input a list of numbers within the field, e.g. 1,2,3,4, ToExpression returns $Failed with the following error message:

ToExpression::notstrbox: 1,2,3,4 is not a string or a box. ToExpression can only interpret strings or boxes as Wolfram Language input.

and Head still outputs:


I want the variable x to be further manipulated numerically with other functions, but if I don't optionally specify the Head of my input for InputField, ContinuousAction won't work; and if I specify the Head not as a String, but as a Number, then I'm unable to input the intended list of numbers, just a single number.


You could use ImportString[] instead:

DynamicModule[{x = ""}, 
              Column[{InputField[Dynamic[x], String, ContinuousAction -> True], 
                      Dynamic[ImportString[x, "List", "LineSeparators" -> ","]],
  • $\begingroup$ Head still outputs String. $\endgroup$ – useranonis Mar 19 '18 at 18:01
  • $\begingroup$ @RichardSantiago obviously because you're not reassigning to x. You can't make x both a String for the InputField and a Number. You have to use two variables for two types. $\endgroup$ – b3m2a1 Mar 19 '18 at 18:02
  • $\begingroup$ @J.M. Ok, so Dynamic[ImportString[x,"List"]]=y, then Dynamic[Head[y]] should work instead? $\endgroup$ – useranonis Mar 19 '18 at 18:05
  • 1
    $\begingroup$ A hint, @Richard: you can't have a Dynamic[] object on the left-hand side of an assignment. With that, as b3m2a1 notes, you need to maintain a second variable to store the converted list. $\endgroup$ – J. M.'s torpor Mar 19 '18 at 18:08
  • $\begingroup$ As an explicit example: DynamicModule[{x = "", xc = {}}, Column[{InputField[Dynamic[x], String, ContinuousAction -> True], Dynamic[xc = ImportString[x, "List"]], Dynamic[Head[xc]]}]]. $\endgroup$ – J. M.'s torpor Mar 19 '18 at 18:15

Dynamic isn't transparent to evaluation. You'll need to bring the ToExpression inside:

Column[{InputField[Dynamic[x], String, ContinuousAction -> True], 
  Dynamic[ToExpression[x]], Dynamic[Head[x]]}]

If you want to manipulate x later then you'll want:

Column[{InputField[Dynamic[x], String, ContinuousAction -> True], 
  Dynamic[xNum = ToExpression[x]], Dynamic[Head[xNum]]}]

Where you can then play with xNum normally.

Note that J.M.'s suggestion to use ImportString[x, "List"] is probably a good one for you here.

  • $\begingroup$ Sadly, it doesn't work. Head still outputs String. $\endgroup$ – useranonis Mar 19 '18 at 18:00
  • $\begingroup$ It doesn't work how? Because ToExpression doesn't work or because there's a bad variable binding? $\endgroup$ – b3m2a1 Mar 19 '18 at 18:01
  • $\begingroup$ I wish I could paste the picture here. I basically copy-pasted your code. I did the same with the first answer I got. For example, an input of 1,3,4 outputs $Failed and String underneath. $\endgroup$ – useranonis Mar 19 '18 at 18:03
  • 1
    $\begingroup$ That's because ToExpression["1, 3, 4"] fails... It's obviously not a valid expression. Try ImportString[x, "List"] instead or supply a valid expression like "{1, 3, 4}". $\endgroup$ – b3m2a1 Mar 19 '18 at 18:04

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.