Set popup menu choices dynamically

I'm trying to create a popup menu that contains choices for the numbers {1,...,numAtoms}, where numAtoms is set in a set variable box. In other words, I need to set the value of the popup box dynamically.

It seems like the ways to do this are extremely limited and I have no idea why this is. I experimentally tried

PopupMenu pickAtom value="asdf"


which worked. Then I tried

String aString = "asdf"
PopupMenu pickAtom value=aString


which did not compile and gave me the error "got 'aString' instead of string variable or string function name" on aString.

I also tried defining a string function "generateMenuStr(numAtoms)", which returned the String I wanted to set the menu's value to. Oddly enough,

print generateMenuStr(numAtoms)
// PopupMenu pickAtom value=generateMenuStr(numAtoms)


prints exactly what I want -- so generateMenuStr(numAtoms) is working without error -- but

print generateMenuStr(numAtoms)
PopupMenu pickAtom value=generateMenuStr(numAtoms)


does not compile and gives me an "unkown/inappropriate name or symbol" error on numAtoms. (This despite the fact that "generateMenuStr" has no trouble accepting numAtoms as an argument in any other context.)

This makes no sense to me and I can think of no way to go around it. This popup menu is an important part of the user interface I'm designing and working without it would be extremely difficult. Can anyone find a way to make this work?
I'm not sure where your error lies but the following works here:
Function/S getList(var)
    variable var
    return "A;B;C;D;" + num2str(var)
End

Function doStuff()
    NewPanel
    PopupMenu popup value=getList(1)
End
thomas_braun wrote:
I'm not sure where your error lies but the following works here:
Function/S getList(var)
    variable var
    return "A;B;C;D;" + num2str(var)
End

Function doStuff()
    NewPanel
    PopupMenu popup value=getList(1)
End


The above works for me too actually, but it doesn't fix my problem...

When I'm trying to set the popup menu's value, the function generateMenuStr I mentioned earlier DOES accept numbers, like the "1" you entered. It DOES NOT accept variables that are themselves numbers.

So when I try

Variable a = 1
PopupMenu pickAtom value=generateMenuStr(a)


this does not compile, because "a" is a variable and not a number. (In fact, this specifically causes the error "An attempt was made to treat a text wave as if it were a numeric wave").
Solved it!

For some reason the string I set the popup menu's value to has to be a global string.

So what worked is:

SVAR menuStr
menuStr = generateMenu(numAtoms)
PopupMenu pickAtom value = menuStr
The documentation for the Popup command is extensive in regard to how the list can be populated.

See the "Setting The Popup Menu Items" section.

But it is perhaps unclear in that description that the popup value is evaluated in a global context and not the function-local context as in your example.

The reason is that the popup exists long after your function has ended, and for the expression to still be valid it must use the only context it still has available: the global context.

--Jim Prouty
Software Engineer, WaveMetrics, Inc.