MATLAB: How to connect two uicontrols so that i can find one via the other

MATLABpairing uicontrol

Hello everybody,
I have a GUI with a number of pairs of edits and popup menus. The popup menus are meant to set the prefix for the dimension of the value written in the edit, e.g. kHz, MHz, GHz:
hPopup = uicontrol('Style','popupmenu','String',{'kHz','MHz','GHz'});
Now if my edit has the value 100 and I change the dimension from 'kHz' to 'MHz' i want the value in the edit to be updated from 100 to 0.1. I surely can achieve that by an appropriate callback. But now I do not have only one such pair, but around 10. One solution is 10 different callbacks, one for each popup menu. But that is ugly. I prefer having one callback for all of them. Ok, this is also possible, but then i may need a switch statement, which does something like
function genericCallback(src,~)
switch src
case hPopupOne
editHandle = hEditOne;
case hPopupTwo
editHandle = hEditTwo;
end
changePrefix( editHandle , src.Value );
end
But this also seems like an unnecessary overhead of duplicate code to me. I like to avoid that. The probably best solution I came up with is kind of a global lookup table like
lookupTable = {...
hPopupOne , hPopupTwo , hPopupThree ;...
hEditOne , hEditTwo , hEditThree};
which I could use to pick the right edit in my callback. However, I am getting the sense that there is an easier and cleaner way to achieve what I intend. I tried to find a solution by searching here and in the internet in general, but I didn't find anything. Probably, just because I don't know the right keyword for this problem. Can anybody help me out?

Best Answer

Hello Benjamin,
What I usually do in situations like this is to set up the callback for the uicontrol as an anonymous function, passing in an extra argument to identify the control that is making the callback. Something like:
h1 = uicontrol(..., 'Callback', @(src, event) myListCallback(src, event, someRelevantVariable1))
h2 = uicontrol(..., 'Callback', @(src, event) myListCallback(src, event, someRelevantVariable2))
The "someRelevantVariable" can be an identifier for the uicontrol (e.g. 'Frequency'), an index in some relevant list, a handle to a different uicontrol you need to use in the callback, etc.
Hope this helps!
-Cam