MATLAB: Setting popup menu value is not updating the variables in the workspace unless I manually click it

menupopupsetupdate

Hi, after using the forum for the past few months I have came across I problem I cannot find a solution to. Hope you can help 🙂
I have two popup menus, the first which is to select a vehicle. I have set that when a vehicle is chosen, the second popup menu will automatically go to the recommended tyre for this vehicle. I am able to make the second popup menu change to the correct selection upon picking a vehicle. However the variables associated with this selection is not updating unless I click on the second popup menu and select the tyre manually.
Can I automatically update all the variables in to the workspace for the second popup menu from only making a selection from the first popup menu. This is the simplified code, I am using set(handles) to set the selection for the second popup menu. But no variables are sent to workspace.
function popupmenu1_Callback(hObject, eventdata, handles)
popupmenu1 = get(handles.popupmenu1,'value');
switch popupmenu1
case 2
set(handles.popupmenu2,'value',2);
case 3
set(handles.popupmenu2,'value',3);
end
Hope this all makes sense. Thanks

Best Answer

Sunny - the line of code
set(handles.popupmenu2,'value',3);
will just change the data for value but won't call the callback associated with changing that value. I think that is what you mean - your popupmenu2_Callback is not being called through the above code when the user selects something from the first popup menu, but (obviously) will when the user selects something from the second popup menu.
What you could do is just take the body for the second popup menu callback and place it in its own function, and have that function called by both popup menus.
Suppose your popupmenu2_Callback is something like
function popupmenu2_Callback(hObject, eventdata, handles)
% do lots of stuff

Take the everything in the body and place it in its own function as
function popupmenu2WorkToDo(hObject,handles)
% do lots of stuff
and call this from popupmenu2_Callback as
function popupmenu2_Callback(hObject, eventdata, handles)
popupmenu2WorkToDo(hObject,handles);
Call this same function from the first popup menu Callback as
function popupmenu1_Callback(hObject, eventdata, handles)
popupmenu1 = get(handles.popupmenu1,'value');
switch popupmenu1
case {2,3}
set(handles.popupmenu2,'value',popupmenu1);
popupmenu2WorkToDo(handles.popupmenu2,handles);
end
Note that in the above, I just combined the two cases (to simplify the code) and then just invoke the function to do all second popup menu "work".
Try the above and see what happens!
Related Question