1
$\begingroup$

For my application, I am using Dynamic functionality to make a number of input fields through which a user can enter numbers. I then take these numbers and place them as the entries in a dynamic matrix. I then use the matrix as an input to a function I've written.

For some reason, I find that Mathematica cannot do anything with a matrix that is formed in this way. Though the matrix seems to be correctly formed, I find that Mathematica will not fully evaluate any expressions involving the dynamic matrix.

Here is some example code

DynamicModule[{numElements = 3, list1,list2},Dynamic@Column[

list1=Table[Symbol["n"<>ToString@i],{i,1,numElements}];
list2=Table[Symbol["t"<>ToString@i],{i,1,numElements}];
{
InputField[Dynamic@numElements],

Dynamic@Column[Flatten@Table[{Row[{With[{i=i},InputField[Dynamic[list1[[i]]],Number]], With[{i=i}, InputField[Dynamic[list2[[i]]], Number]]}]}, {i,1,numElements}]],

stack = Table[With[{i=i}, {Dynamic[list1[[i]]],Dynamic[list2[[i]]]}], {i,1,numElements}], Button["Press to find max", Print@Max[stack]]}
]]

This generates a simple user interface that allows you to control two lists list1 and list2 with entries n1, n2, n3, etc. and t1, t2, t3, etc., and it allows you to control their length. It then takes these values and makes a 2 x numElements matrix.

However, it doesn't seem like I can do anything with this matrix. Above I tried to print out its maximum value through a button. What could be the issue here?

$\endgroup$
  • 1
    $\begingroup$ In all likelihood you have Dynamic positioned in the wrong place but we need to see your code. $\endgroup$ – Mike Honeychurch Jan 27 '15 at 6:21
  • $\begingroup$ Thanks, guys. I just updated it with an example of the issue. $\endgroup$ – Noah Rubin Jan 27 '15 at 6:24
  • 1
    $\begingroup$ @NoahRubin The example doesn't work for me, but the problem is what Mike said. You cannot evaluate Max[Dynamic[val1],Dynamic[val2]], it has to be Dynamic@Max[val1,val2]. The issue is similar to lite item six here. Once you wrap your number with Dynamic it doesn't go away. And it is not possible to determine if Dynamic something is larger than Dynamic something else, these are not numeric values. There is more about this in the documentation, if I recall correctly there is a paragraph specifically about where to place Dynamic. $\endgroup$ – C. E. Jan 27 '15 at 6:46
  • $\begingroup$ There was one misplaced ] that creeped in there, I fixed it now and it should work. Thanks for your replies. If I need to do Dynamic@Max[val1,val2], how do I get that to evaluate when val1 and val2 need to be dynamic variables themselves, as in this example? It seems I can't actually do anything with the matrix of dynamic variables, even a multiplication of the matrix by 5, say, doesn't evaluate. $\endgroup$ – Noah Rubin Jan 27 '15 at 14:43
1
$\begingroup$

Try

Button["Press to find max", Print@Max[{list1, list2}]]

for your button.

$\endgroup$
  • $\begingroup$ Thanks so much! At first I thought this was unrelated to my question, but it actually led me down a path to finding a solution to my problem. $\endgroup$ – Noah Rubin Jan 29 '15 at 1:52
  • 1
    $\begingroup$ @NoahRubin You're welcome. If your solution is significantly different, consider posting your own answer. $\endgroup$ – Michael E2 Jan 29 '15 at 1:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.