1
$\begingroup$

I am trying to create a dialog box that informs the user when the input is wrong (or even when any type of error occurs) in the manipulate function.

I have the following code:

Plotter[function_] := 
Plot[function, {x, -1, 1}, PlotLabel -> Style [function, Bold]];
Manipulate[
Quiet[Plotter[f]], {{f, Sin[x], 
Text[Style["Function:", Bold, Medium]]}, {Sin[x], Tan[x]}
}, Text[Style["\nType the function:", Bold]], 
Panel[DynamicModule[{var = "Function"}, InputField[Dynamic[f]]]], 
ControlPlacement -> Left, ControlType -> PopupMenu, 
SaveDefinitions -> True, SynchronousInitialization -> False]

And I implemented the dialog box in the following way:

Plotter[function_] := 
Plot[function, {x, -1, 1}, PlotLabel -> Style [function, Bold]];
Manipulate[
Quiet[Check[Plotter[f], {MessageDialog["Error."], Abort[]}]], {{f, 
Sin[x], Text[Style["Function:", Bold, Medium]]}, {Sin[x], Tan[x]}
}, Text[Style["\nType the function:", Bold]], 
Panel[DynamicModule[{var = "Function"}, InputField[Dynamic[f]]]], 
ControlPlacement -> Left, ControlType -> PopupMenu, 
SaveDefinitions -> True, SynchronousInitialization -> False]

which seems to work up until the point when I try to type another function. The plotting does not work and the error box does not appear anymore when the input is wrong. Why does this happen? And how to I fix this? I think that the Abort[] function causes this, but if I do not put it there, the message box appears indefinitely.

I created the error with the user input "Sin[x,y]".

$\endgroup$
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – Michael E2 Jan 20 '18 at 2:03
2
$\begingroup$

Maybe this?

Plotter[function_] := Plot[function, {x, -1, 1}, PlotLabel -> Style[function, Bold]];
Manipulate[
 Quiet[(fold = f; #) &@                     (* set fold to new f if valid *)
   Check[Plotter[f],
    {MessageDialog["Error."]; f = fold}]],  (* reset invalid f *)
 {{f, Sin[x], Text[Style["Function:", Bold, Medium]]}, {Sin[x], Tan[x]}},
 {{fold, Sin[x]}, ControlType -> None},     (* keep previous valid f *)
 Text[Style["\nType the function:", Bold]], 
 Panel[DynamicModule[{var = "Function"}, InputField[Dynamic[f]]]], 
 ControlPlacement -> Left, ControlType -> PopupMenu, 
 SaveDefinitions -> True, SynchronousInitialization -> False]
$\endgroup$

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.