Message Boards Message Boards

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

Posted 10 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

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
POSTED BY: Bianca Eifert
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