5
$\begingroup$

I am trying to access the values of a dynamic value outside function package. Within the function package, the function is defined under the Dynamic module option. There is an error with this. I cannot extract the value.

inputCreate[no_, user_values_] := (DynamicModule[{value = Table[0, {no}, {user_values}]},
   (If[(no > 0),
     ({Table[(With[{i = i, j = j},
          (Row[{Column[{MouseAppearance[
                EventHandler[
                 Style["\[FilledUpTriangle]", Bold, Black, {15, 15}, 
                  ShowStringCharacters -> 
                   False], {"MouseClicked" :> (value[[i, j]] = 
                    value[[i, j]] + 1)}],
                "LinkHand"],
               MouseAppearance[
                EventHandler[
                 Style["\[FilledDownTriangle]", Bold, Black, {15, 15},
                   ShowStringCharacters -> 
                   False], {"MouseClicked" :> (If[(value[[i, j]] <= 
                    0), (value[[i, j]] = 0), (value[[i, j]] = 
                    value[[i, j]] - 1)])}],
                "LinkHand"]},
              Spacings -> 0, Alignment -> Center, 
              Background -> RGBColor[0.65, 0.65, 0.65]], 
             Dynamic[If[(value[[i, j]] === Null), (value[[i, j]] = 0)];
              InputField[Dynamic[value[[i, j]]], Number, 
               ContinuousAction -> True, ImageSize -> {50, 20}, 
               Alignment -> Center]]}])]),
        {i, 1, no}, {j, 1, Length[Part[value, i]]}],
       Dynamic[value = If[value === Null, 0, value]]})
     ])])

Mathematica graphics

When I deploy in CDF, this function works but I am unable to extract the value of myInput that I need to write into another file. Also, in the CDF, when I delete the zero and add click the uparrow, the value keeps incrementing + NUll whereas everytime I want to reset it to zero when the user deletes the values within the InputField.

So, just to extract the values, I implemented the same function with Module. However, within module, the initial value is not set to zero even though I have explicitly initialized it within the module.

$\endgroup$
10
  • 1
    $\begingroup$ Welcome to Mathematica.SE, Anuk. I formatted the code for you as it did not display very well. You better not use tabs for aligning code. Also, don't use quoting of code for code blocks but only for inline code. Further help on Markdown can be found here $\endgroup$ Commented Jan 27, 2013 at 13:04
  • $\begingroup$ @SjoerdC.deVries, Thanks for the welcome, I will definitely follow the formatting rules from next time on wards. I am glad to be a part of Mathematica group. Thanks and regards, Anuk $\endgroup$
    – Anuk
    Commented Jan 27, 2013 at 13:07
  • $\begingroup$ InputFields don't work in CDFs. Also, you can't write to files in CDFs. See this documentation, section "Interactivity in .cdf Files". $\endgroup$ Commented Jan 27, 2013 at 13:50
  • $\begingroup$ I just tested your CDF and it worked OK in the signed version. The problem is with the free version. $\endgroup$
    – Murta
    Commented Jan 27, 2013 at 13:58
  • 1
    $\begingroup$ @Sjoerd: it looks like the various sources are not completely in sync. I found that numeric InputFields are supported here details for programmers and numeric InputFields indeed do work in the free CDF-Player... $\endgroup$ Commented Jan 27, 2013 at 19:52

1 Answer 1

2
$\begingroup$

I have mentioned in a comment that I think your question needs to be improved if you want more useful answers, mine is to a large extent based on guesses what you might actually want to achieve. I believe that you try to implement a gui element or widget that is to my knowledge known as a spinner or spinbox in other languages/frameworks. Then there is the complication that you want to use such a widgets to set the entries of an array. I think the following should solve the problems that you have mentioned in your question. None of these seems to me to have to do anything with CDF and the above solution should (and with my limited testing does) work within the free CDF player. Finally you mention that you "need to write into another file". It is honestly completely unclear to me what that should mean and is the only functionality I see that probably isn't possible with a free CDF document.

spinner widget

writing such a widget could actually be achieved with somewhat less effort as what I do present, but the following can also be used to set values in an array and implements some additional convenience, which makes things slightly more complicated. There is still a lot of possible improvement concerning robustness, style, flexibility etc.

ClearAll@spinner

spinner[Dynamic[var_]] := spinner[Dynamic[var, (var = #;) &]]

spinner[Dynamic[valexpr_, setfun_]] := (
   Row[{
     EventHandler[
      InputField[
       Dynamic[valexpr, If[# === Null, setfun[0], setfun[#]] &],
       Number,
       ContinuousAction -> True, ImageSize -> {50, 20}, 
       Alignment -> Center
       ],
      {
       "UpArrowKeyDown" :> setfun[Replace[valexpr + 1, {
           _?Negative :> 0,_?(Not[NumericQ[#]] &) :> 0,x_ :> Round[x]
           }]],
       "DownArrowKeyDown" :> setfun[Replace[valexpr - 1, {
           _?Negative :> 0,
           _?(Not[NumericQ[#]] &) :> 0,
           x_ :> Round[x]
           }]]
       }
      ],
     Column[{
       MouseAppearance[
        EventHandler[
         Style["\[FilledUpTriangle]", Bold, Black, {15, 15}, 
          ShowStringCharacters -> 
           False], {"MouseClicked" :> (setfun[Replace[valexpr + 1, {
               _?Negative :> 0,_?(Not[NumericQ[#]] &) :> 0,x_ :> Round[x]
               }]])}
         ],
        "LinkHand"
        ],
       MouseAppearance[
        EventHandler[
         Style["\[FilledDownTriangle]", Bold, Black, {15, 15}, 
          ShowStringCharacters -> False],
         {"MouseClicked" :> (setfun[Replace[valexpr - 1, {
               _?Negative :> 0,
               _?(Not[NumericQ[#]] &) :> 0,
               x_ :> Round[x]
               }]])}],
        "LinkHand"
        ]
       },
      Spacings -> 0, Alignment -> Center, 
      Background -> RGBColor[0.65, 0.65, 0.65]
      ]
     }]
   );

here is how you could use it:

{spinner[Dynamic[x]], Dynamic[x]}

arrays of contols

The second problem you are facing is that you have an array of values and want to have a single widget for any of them. That is a known problem addressed several times in other questions and there is a standard trick to solve it: use With to insert the evaluated indices into the unevaluated dynamic expression, as shown here:

DynamicModule[{values = {{1, 2, 3, 4}, {5, 6, 7, 8}}},
 Column[{
   Grid[Table[
     With[{r = r, c = c}, spinner[Dynamic[values[[r, c]]]]],
     {r, Length[values]},
     {c, Length[values[[1]]]}
     ]
    ],
   Dynamic[values]
   }]
 ]

And this is how that will look like:

Mathematica graphics

$\endgroup$
2
  • $\begingroup$ Thanks for the answer. I got what I was looking. Thank you again. It works fine.! $\endgroup$
    – Anuk
    Commented Jan 29, 2013 at 11:45
  • $\begingroup$ @Anuk: good to hear it did help you. If you want this to be more useful for others you still might want to reformulate your question and the title so others have a better chance to find it... $\endgroup$ Commented Jan 30, 2013 at 10:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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