Set popup menu choices dynamically
dtadams
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
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)
// 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)
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?
variable var
return "A;B;C;D;" + num2str(var)
End
Function doStuff()
NewPanel
PopupMenu popup value=getList(1)
End
July 29, 2014 at 12:30 pm - Permalink
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
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").
July 29, 2014 at 02:05 pm - Permalink
For some reason the string I set the popup menu's value to has to be a global string.
So what worked is:
menuStr = generateMenu(numAtoms)
PopupMenu pickAtom value = menuStr
July 29, 2014 at 02:15 pm - Permalink
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.
July 29, 2014 at 09:22 pm - Permalink