2
$\begingroup$

I have a DynamicModule in which I go through some data analysis and keep or reject the results. Here is a toy example of what I am doing.

ClearAll[f];
SetAttributes[f, HoldFirst];
f[{ip_, op_}] := DynamicModule[{a, b},
   a = ip;
   Column[{
      Dynamic[b = Total[a]],
      Row[{
       Button["keep", AppendTo[op, {ToString[Unevaluated[ip]], b}]],
       Button["reject", AppendTo[op, {ToString[Unevaluated[ip]], Null}]]
      }]
  }]
] 

I make 10 sets of data

data = Table[RandomReal[{-1, 1}, 10], {10}];

I now run my module on the first set using

op = {};
f[{data[[1]], op}]

Which gives me

Mathematica graphics

When I look at my output I have

op

Mathematica graphics

Which is what I want. This output records the data set and the result. If I do this again with all the data using a table I can still proceed

op = {};
Column[Table[f[{data[[n]], op}], {n, Length[data]}]]

Mathematica graphics

I press my buttons. However when I now look at my output all the records are data[[n]], data[[n]], data[[n]]... instead of data[[1]], data[[2]], data[[3]]...

op // TableForm

Mathematica graphics

What I want is this

Mathematica graphics

How can I make the n in the output of data[[n]] change to reflect the input number? Thanks for any suggestions.

| improve this question | | | | |
$\endgroup$
  • $\begingroup$ With can inject into held expressions: Column[Table[With[{n = n}, f[{data[[n]], op}]], {n, Length[data]}]] $\endgroup$ – Kuba Sep 18 '14 at 18:56
  • $\begingroup$ @Kuba A good workaround. Many thanks. Pity I can't include this in the module somehow... $\endgroup$ – Hugh Sep 19 '14 at 8:55
1
$\begingroup$

As Kuba suggested in his comment, With is helpful for accomplishing what you ask for.

ClearAll[f];
SetAttributes[f, HoldFirst];
f[ip_] :=
  Module[{b},
    Column[{b = Total[ip], 
      Row[{
        Button["keep", AppendTo[op, {ToString[Unevaluated[ip]], b}]],
        Button["reject", AppendTo[op, {ToString[Unevaluated[ip]], Null}]]}]}]]

SeedRandom[42]; data = RandomReal[{-1, 1}, {4, 10}];
op = {};
Column[Table[With[{n = n}, f[data[[n]]]], {n, Length[data]}]]

output

op
{
  {"data[[1]]", -1.47076}, 
  {"data[[2]]", Null}, 
  {"data[[3]]", 3.35806}, 
  {"data[[4]]", 2.33047}
}

To reduce the size of my answer, I have reduced the data to four lists of ten real numbers and omitted the formatting of the output. I have also done some simplification on your definition of f. In particular, I eliminated the use of dynamic constructs as they are not needed for what you are doing.

| improve this answer | | | | |
$\endgroup$
  • $\begingroup$ Thanks for this. I think this solves my problem although it makes using the module more complicated. The simplifications to my module are also worth considering. This could leave to another question... $\endgroup$ – Hugh Sep 19 '14 at 8:55

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.