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};
];
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:
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.
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.
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.
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.
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"]
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.
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.
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.
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.
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.