Enable and populate popup menu programatically?

I'm building a panel where I use one popup menu to select from a series of experiments, each of which has either 1, 2, or 3 replicates. I want to enable and populate another popup menu with the replicate number. All the popup menus are pre-created when the panel is initiated, and the replicate popup is disabled until an experiment is chosen.

How do I manipulate an already-existing control? I can't find anything about this anywhere!

Just call PopupMenu again, preferably with the win= keyword attached to target the right panel, then append the option you want to modify (I guess value= in this case). This works the same with all controls. Note that you must use the same name of the control, otherwise you create a new popup control.

In reply to by chozo

chozo wrote:

Just call PopupMenu again, preferably with the win= keyword attached to target the right panel, then append the option you want to modify (I guess value= in this case). This works the same with all controls. Note that you must use the same name of the control, otherwise you create a new popup control.

Brilliant, thank you.

Edit: maybe you can help me with this problem. I've got this in a function:

variable numReplicates = ItemsInList(matchX, ";")
if (numReplicates == 0)
     print "No replicate data found for this experiment."
else
     PopupMenu xSelectPopup,win=smp0,value=#(makeValueString(numReplicates)),disable=0,popvalue="1"        
     UpdateGraph()
endif

and the function makeValueString:

function/s makeValueString(n)
    variable n
    variable i
    string nr = ""
    for (i=1; i<=n; i+=1)
        nr += num2str(i)+";"
    endfor
    return nr
end

It doesn't work. It compiles, and I've verified that the makeValueString() function is returning what I want, but the popup contains "_none_".

The usage of # requires a string in a specific style. In particular, you need to add a literal quote character:

variable numReplicates = ItemsInList(matchX, ";")
if (numReplicates == 0)
     print "No replicate data found for this experiment."
else
     String select = "\"" + makeValueString(numReplicates) + "\""
     PopupMenu SelectPopup,win=smp0,value=#select,disable=0,popvalue="1"        
     UpdateGraph()
endif

A few other notes:

  • Wouldn't you want to update / disable the popup if numReplicates == 0?
  • Note that the popvalue key sets the content of the popup literally to "1" even if this is not part of your input list. I guess in your case at least "1" is always present, but you might want to keep this in mind.

It's possible that instead of the `popValue` keyword, perhaps you want `mode`, which selects the item number to show. If you have a menu with value="1;2;3;", then setting `mode=1` selects the first item, which by coincidence happens to match the item number.

Frankly, the `popValue` keyword has a very specific and obscure purpose. It is generated in recreation macros so that if conditions prevent the generation of a menu value list that includes the selected item, the menu will still look like it did when you saved the experiment. Personally, I would prefer that the menu actually reflect current conditions. I would get rid of the `popValue` keyword if I could, but there are thousands of recreation macros that would stop working.