2
$\begingroup$

I'm trying to use Manipulate to specify a list of numbers on the user side, say {50,60,70,80,90,95,99}, but I don't want the curly braces to need to be in the text entry box. i.e.

Manipulate[
(*my code here*),

{{variable,{50,60,70,80,90,95,99}},InputField}
]

Has the functionality I want, but I would like to get rid of the curly braces that are displayed in the text entry field.

$\endgroup$
2
  • $\begingroup$ Whoops, had to make a small edit to the code. $\endgroup$ – Daniel Mar 1 at 23:11
  • $\begingroup$ Manipulate[(*my code here*)variable, {variable, {50, 60, 70, 80, 90, 95, 99}}] ? $\endgroup$ – cvgmt Mar 1 at 23:26
2
$\begingroup$

I prefer using the tracking function in Dynamic, which makes managing TrackedSymbols easier in Manipulate.

Manipulate[
 variable,
 {{variable, {50, 60, 70, 80, 90, 95, 99}}, InputField[
    Dynamic[
     StringTrim[ToString@variable, "{" | "}"],
     With[{v = ToExpression["{" <> # <> "}"]},
       If[ListQ@v,
        variable = v,
        (* raise exception *)
        ]
       ] &
     ],
    String] &}
 ]
$\endgroup$
1
$\begingroup$

I believe to maximize the experience for the end users, the best way would be to employ the API related functionalities. You may see the following entries:

You may try using the following construct:

Manipulate[Module[{internalvar},
  internalvar=
  Interpreter["Number" | DelimitedSequence["Number"] | 
              DelimitedSequence["Number", {"{", ",", "}"}]][variable];
  (*your code here*)
 ], {{variable, 
  ToString@{50, 60, 70, 80, 90, 95, 99}}, 
 InputField[#, String]&
}]

This shall provide a robust interface, for many sort of input.

Screenshots of results: Input a valid Wolfram list Input with out brackets Input as space separated sequence

$\endgroup$
0
$\begingroup$

Welcome to MMA SE! So, one way to do this is to actually treat the manipulated variable as a string, and then parse it into your "real" variable in the code.

So, to do that, we're going to make two quick functions: one to convert lists to strings-without-brackets, and one to convert back.

ListToString[l_List] := StringTake[ToString[l], {2, -2}]

StringToList[s_String] := ToExpression["{" <> StringTrim[s,","] <> "}"]

In the first, it takes characters 2 to the penultimate one (-2), thus excluding the braces at either end, and in the second, it string-joins (<>) curly braces at either end and then parses it as a Mathematica expression via ToExpression.

You might be wondering why StringTrim[s, ","] is in there; that's just to trim off any trailing commas if your user forgets! (E.g. if their input is "1,2,3," and they forgot to delete the last comma; this will take care of that behind the scenes for them.)

So, inside the Manipulate, at the start of the code labeled (*my code here*) in your question, you'll want to insert the following:

realvariable = StringToList[strvariable]; (*my code here*)

This will parse your input whenever your user presses Enter.

Now change your Manipulate variable specification to

{{strvariable, ListToString[{50,60,70,80,90,95,99}]}, InputField[#, String] &}

Using InputField[#, String] & will make `InputField format the string nicely, without quotes.

Also, as a note: it would be wise to put this realvariable in a Module, so that the variable realvariable is trapped inside the manipulate, e.g.

Manipulate[
  Module[{realvariable},
    realvariable = StringToList[strvariable];
    (*my code here*)
    ],
  {{strvariable, ListToString[{50,60,70,80,90,95,99}]},
    InputField[#, String] &}
  ]

Let me know if it's unclear how any of this works!

$\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.