Message Boards Message Boards

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

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

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