Message Boards Message Boards

How To Create a Text Field for Manipulate User Input for a File Path

Posted 11 years ago

I am trying to do two things:

1 - Add a Manipulate text field input to an existing notebook to be used for entering a string for a file path. That file path is to be used as the location for a file to import into the notebook.This notebook then plots data from the input file.

2 - Export the notebook for CDF Player Pro allowing the user to specify their own file path for the data file to plot in the notebook.

So for 1, the documentation on InputField and InputField Dynamic seems to generate a text box in the notebook but it is not clear how to assign the contents of the text field to a variable. The output looks like it just goes to another text box.

Is there a way to have a Text Box Input with the contents of that text box (in this case a string) assigned to a notebook variable which can then be used as a file path argument to the "FindList" function? For2 we then want to export this functionality to a CDF file for other users to use in CDF Player Pro.

See example output below.

{InputField[Dynamic[x]], Dynamic[x]}
x
(* InputField[20, Number] *)

enter image description here

Here is the example output.

POSTED BY: Bob Stephens
2 Replies

Hi Bob,

it looks as though you evaluated this:

{InputField[Dynamic[x]], Dynamic[x]}

Next you seem to have entered "40" into the input field, then evaluated the output it produced, thus yielding another input field in your "Out" line. You don't actually have to do that, but you simply evaluate the original input, and when you enter something into the input field, it is assigned to x automatically. You never even hit "enter" from inside the input field. (There are actually ways to suppress output when a user accidentally does this, but the method isn't pretty and it may not be necessary if the input field is essentially inside a Manipulate.)

I'm not sure if you're aware that you don't actually need the second "Dynamic[x]". In the documentation, the Dynamic[x] is used to show that x does, in fact, change when you type something in the input field, but it doesn't have anything to do with setting the variable x. So usually you'd simply use this:

InputField[Dynamic[x]]

... which evaluates to an input field which assigns its contents to the variable x (or whatever you choose to call it). The input field really behaves like any other dynamic control element you might have in your code (sliders and setters and so forth).

For file names specifically though, you should take a look at FileNameSetter ( http://reference.wolfram.com/language/ref/FileNameSetter.html ), it's a lot more comfortable for most users than an input field.

I don't know exactly what kind of constructs Player Pro supports, some things may behave differently from a regular notebook. But I hope I could still help you a little.

POSTED BY: Bianca Eifert

If you want to, for example, set a parameter called filePath to the path to a file on your system you could use a button to execute the action as in:

Button["Set filePath",
 filePath = SystemDialogInput["FileOpen"],
 Method -> "Queued"]

Then, when the button is clicked the parameter filePath is set to the path to the file that you selected. You might then put this button in a DynamicModule to localize filePath and have your additional code (i.e., to plot data contained in the file) as a dynamic element within that DynamicModule.

Here is an example of something like that.

First create a test file on your disk using something like:

Export["/Users/dreiss/Desktop/testdata.txt", Range[10], "Table"]

Then experiment with the following

DynamicModule[{filePath, data = {3, 2, 1}},
 Column[{
   Button["Set filePath",
    (
     filePath = SystemDialogInput["FileOpen"];
     If[filePath === $Canceled, data = RandomReal[{0, 10}, 20], 
      data = Import[filePath, "List"]]
     ),
    Method -> "Queued"],
   Dynamic@ListPlot[data]
   }
  ]
 ]
POSTED BY: David Reiss
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract