0
$\begingroup$

I want to request user input of an expression of f(x) from a dialog and then use ToExpression on the input.

Questions:

  1. When I execute the code below, I got r before I click the button. So that I get r before I actually enter the expression. Is it because I'm using Dynamic[input]?
  2. How to execute the last line after I hit the button?

x = Table[i, {i, 20}];
input = "";
CreateDialog[
  Grid[
    {{"f(x)=", InputField[Dynamic[input], String]},               
     {"", "Input expression f(x)"},
     {"", DefaultButton["OK", DialogReturn[ret = input]]}},
    Spacings -> {0.5, Automatic}, 
    Alignment -> Center
  ],
  Modal -> True, 
  WindowTitle -> "Input information"];
r = ToExpression[ret, TraditionalForm];
$\endgroup$
1
$\begingroup$

Not sure why you define x and then never use it but despite that try this:

ret = DialogInput[
   DialogNotebook[{
     DynamicModule[{input},
      Grid[{{"f(x)=", InputField[Dynamic[input], String]}, {"", 
         "Input expression f(x)"}, {"", 
         DefaultButton["OK", DialogReturn[input]]}}, 
       Spacings -> {0.5, Automatic}, Alignment -> Center]
      ]
     }],
   ShowStringCharacters -> False,
   WindowTitle -> "Input information"];
| improve this answer | |
$\endgroup$
  • $\begingroup$ Hi Mike, sorry about the confusion. I was trying to use the input expression to manipulate vector x. Your answer solved my question perfectly. Thank you very much! $\endgroup$ – Hayward Li Apr 2 '16 at 18:38

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.