Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #9252 > unrolled thread
| Started by | Don <donabc@comcast.net> |
|---|---|
| First post | 2012-03-28 10:01 +0000 |
| Last post | 2012-03-29 07:57 +0000 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
Input vs. InputField for Interactive Applications Don <donabc@comcast.net> - 2012-03-28 10:01 +0000
Re: Input vs. InputField for Interactive Applications A Retey <awnl@gmx-topmail.de> - 2012-03-29 07:57 +0000
| From | Don <donabc@comcast.net> |
|---|---|
| Date | 2012-03-28 10:01 +0000 |
| Subject | Input vs. InputField for Interactive Applications |
| Message-ID | <jkunhb$aso$1@smc.vnet.net> |
I am trying to do something very simple.
At a place inside a function, I am asking the user
to input the lower case letter a (for automatic) or
a series of integers (representing months from Jan. to Dec.
The integer numbers can be separated by commas AND/OR spaces) .
After the user has input either the lower case a
OR a series of integers in the range 1 to 12 inclusive,
the function takes this input and performs certain
actions based upon the input from the user.
That's it ! That's the whole problem.
I have two choices with Mathmatica, it appears,
to do the simple task outlined above:
(1) I can use the Input function. The trouble with this is that
a series of integers looks like a multiplicaiton to Mathematica
and it wants to multiply the integer numbers - clearly not
what I need as input.
(2) I can use the InputField function. The troulbe with this
is that it puts out an input field, but then doesn't wait
for the user to input the list of integers. It immediately
goes to the next line of code and executes that, and so on.
I need a hybrid of the Input and InputField functions.
Examples of what's wrong with the Input
and InputField functions are demonstrated below.
Thank you for any help you can give me.
Don
User of Input function:
Remove[test];
test[] := Module[{},
input = Input["Input a series of montly integers OR the lower
case a [for automatic] to leave out all months with an Avg. \
Negative TR
and then hit OK"];
];(* End Module test *)
test[]
Use of InputField function:
Remove[test1, s];
test1[] := Module[{},
s = "";
Print[Column[{InputField[Dynamic[s], String,
FieldHint -> "Enter series of integers for the months
separated by spaces or commas"]}]];
response =
Dynamic["{" <>
StringReplace[StringReplace[s, "," -> " "],
Whitespace -> ","] <> "}" // ToExpression // Flatten];
Print["test1 functon has not waited for the input"];
];(* End Module test1 *)
test1[]
[toc] | [next] | [standalone]
| From | A Retey <awnl@gmx-topmail.de> |
|---|---|
| Date | 2012-03-29 07:57 +0000 |
| Message-ID | <jl14le$l71$1@smc.vnet.net> |
| In reply to | #9252 |
Hi,
> At a place inside a function, I am asking the user
> to input the lower case letter a (for automatic) or
> a series of integers (representing months from Jan. to Dec.
> The integer numbers can be separated by commas AND/OR spaces) .
>
> After the user has input either the lower case a
> OR a series of integers in the range 1 to 12 inclusive,
> the function takes this input and performs certain
> actions based upon the input from the user.
>
> That's it ! That's the whole problem.
>
> I have two choices with Mathmatica, it appears,
> to do the simple task outlined above:
>
> (1) I can use the Input function. The trouble with this is that
> a series of integers looks like a multiplicaiton to Mathematica
> and it wants to multiply the integer numbers - clearly not
> what I need as input.
>
> (2) I can use the InputField function. The troulbe with this
> is that it puts out an input field, but then doesn't wait
> for the user to input the list of integers. It immediately
> goes to the next line of code and executes that, and so on.
>
> I need a hybrid of the Input and InputField functions.
You need a custom "modal" dialog, which can be programmed with
DialogInput. Here is one way, which still misses some additional checks
which are due before calling ToExpression on just "something" the user
types :-). Also not that DialogInput (just as Input) can eventually
return $Canceled, when the window is closed without clicking the OK
button...
test[] := Module[{response, values},
response = DialogInput[{
DynamicModule[{str = ""}, Column[{
InputField[Dynamic[str], String],
DefaultButton["OK", DialogReturn[str]]
}]
]
}];
Switch[response,
"a",
values = Automatic,
_String,
values =
ToExpression[StringSplit[response, (Whitespace | ",") ..]],
_,
values = {};
]
]
You might want to have a look at this: tutorial/CreatingDialogBoxes. I
also think you should have a look at tutorial/IntroductionToDynamic and
maybe tutorial/AdvancedDynamicFunctionality if you need further insight.
Another side note: I would strongly recommend to separate the function
which does the calculation from the interaction with the user, e.g. by
providing the result of the above function as an input to the
calculating function or call the above from the calculating function
only when the input wasn't given as an argument. There are various good
reason to do that, but most obviously you'll start to hate the dialog
once you are testing and debugging the actual calculation...
hth,
albert
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web