Message Boards Message Boards

0
|
4415 Views
|
18 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Multiple InputFields

Posted 10 years ago

Hi, I was trying to write a code so as to have output multiple dynamic input fields, however when I try to input one field it automatically puts the same value in the other fields, I need them to be input separately. Thanks!

n = 2;
For[i = 0, i <= n + 1, i++
    {Print["\[Rho][", i, "]"], 
     InputField[Dynamic[\[Rho][i]]] // Print};
    ];
POSTED BY: Al Guy
18 Replies
Posted 10 years ago

Might it be it's a conflict of multiple dynamics?

POSTED BY: Al Guy

You should not use pure functions here since once you execute the checkbos, the following dynamic evaluates the c[Sigma][i] before putting it into the Dynamic. It then is a value rather than an expression.

POSTED BY: David Reiss
Posted 10 years ago
 InputField[Dynamic[#], FieldSize -> 5] &[n]

 Dynamic@If[NumberQ[n], 
   Grid@Table[{"\[Rho][" <> ToString[i] <> "]", 
      InputField[Dynamic[#], FieldSize -> 5] &[\[Rho][i]], 
      Checkbox[Dynamic[#]] &[c\[Rho][i]], 
      "\[Sigma][" <> ToString[i] <> "]", 
      InputField[Dynamic[#], FieldSize -> 5] &[\[Sigma][i]], 
      Checkbox[Dynamic[#]] &[c\[Sigma][i]]}, {i, 0, n}] ]


 Dynamic[c\[Rho][1]]

When I put them in the package with Print@ it returns an error. So How can I print this from the package or some outside source? It Doesn't work in notebook either.

POSTED BY: Al Guy
Posted 10 years ago

Thanks! I wanted to create a checkbox , so when it's checked the variable witll be assigned the value of True and False otherwise, however it returns error.

Checkbox[Dynamic[#]] []] Dynamic[#] []]

Probably I should use radiobutton instead.

POSTED BY: Al Guy

Generally the idea of a package is to define functions that you will then call after the package is loaded. But in any event, what is returned when you load a package using either Needs or Get is the result of the last expression that was executed when loaded.

You can certainly use Print in your package code, but it is not advised. It is best to use an approach to architecting your code in Mathematica so as to avoid this approach. It will lead to odd behaviors since you'd be trying to make Print do things it was not designed for.

POSTED BY: David Reiss
Posted 10 years ago

Another thing is that I need to output something from the package i write, but unless I use print , it doesn't output into the notebook from where i call the package.

POSTED BY: Al Guy
Posted 10 years ago

I understand I should not use Print, but why doesn't it work here? It works without Print

POSTED BY: Al Guy

First, I do recommend that you move away from using Print statements. The expression following expression (with out the semicolon) will create the InputField in the output cell that results.

InputField[Dynamic[#], FieldSize -> 5] &[n]

Your second line (aside from it having a Print statement ;-) ) does not work [artially because Dynamic@NumberQ[n] never evaluates as True or False. It is always an expression of the form Dynamic[...]. The sort of thing you are looking for is like the following:

Dynamic@If[NumberQ[n], "I am a number", "I am not a number"]
POSTED BY: David Reiss
Posted 10 years ago
POSTED BY: Al Guy

Again, I am not sure if you are encountering a bug or the way Enter is used on Windows.

However, generally you do not want to use Enter in the input fields. You just typ in content and the dynamic values are updated. To move from one input field to another you use Tab or click in the new input field. When you do either of these the dynamic values will be updated.

POSTED BY: David Reiss
Posted 10 years ago

And this happens only when I use the num keys' enter.

POSTED BY: Al Guy
Posted 10 years ago

I am on Windows 7, I restarted it, still same.

POSTED BY: Al Guy

Hmmmm.... I do not see that behavior on my system: Mathematica 10 on Mac OSX.

POSTED BY: David Reiss
Posted 10 years ago

But I just type a number and press enter, without any shift, and it still does that.

POSTED BY: Al Guy

If you press Shift-Enter (or type anything else aside from in an input field) in an Output cell that cell will turn int to an Input Cell. The InputFields that you have created are not intended to be used as an input to be executed using Shift-enter. The when you type content in them they will dynamically update the value of the parameters that they dynamically contain.

POSTED BY: David Reiss
Posted 10 years ago

There seems to be some minor issue. When it outputs the fields and I enter number and press "Enter", it takes the whole thing as input and outputs the same fields again. How can I overcome this? Thanks.

POSTED BY: Al Guy
Posted 10 years ago

Thank you very much, I appreciate!

POSTED BY: Al Guy

First off, I recommend that you work your way towards writing things in a less procedural way--as a general rule it doesn't pay to use things Like For, Do, or While in Mathematica. Also, outputting your results should not be done via a Print statement--best to reserve Print as a debugging tool at best.

That said, the reason why you weren't able to separate your Dynamic InputFields from one another is that Dynamic has the Attribute HoldAll. In essence this means that, in your code, each of your Dynamic has a raw [Rho][i] inside of it--without the i having been assigned a value.

Here is an approach, based on your original code that achieves your original intent (there may well be better ways to do this) and which uses Mathematica sorts of functions rather than a For:

Grid@Table[{"\[Rho][" <> ToString[i] <> "]", InputField[Dynamic[#]] &[\[Rho][i]]}, {i, 0, 4}]

The trick here to get the [Rho][i] inside of the Dynamic was to use a pure function to get it evaluated (and hence the i getting a value at each step) outside of the Dynamic before being inserted into the place where you want it.

Be careful of this approach though. If [Rho] is a function with a definition, then [Rho][i] will evaluate to whatever value it evaluates to before getting into the Dynamic. So perhaps it is best to execute

ClearAll[\[Rho]]

before evaluating the above code.

There are a few Mathematica coding lessons in this explanation which will help you learn some useful things if you read up on them.

I hope this helps....

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