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

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

1 Comment

hey, can you tell me how to change the second pop up menu with respect to the first one?
I have been working on state-district plot, so need to chnage district values in accordance to the state which is in the first pop up.
function Checkforchange(hObject,eventdata, handles)
tf=hasChanged(select1); % select1 is my selected value, i used get(handles.popupname,'string') to check what was selected.
if tf==1
setpopupString2(hObject,eventdata, handles) %calling function to set popup string
end
Is this correct?
thank you

Sign in to comment.

 Accepted 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!

4 Comments

Hi, firstly thanks for taking the time to reply. I have set my code exactly as you have described above but I am getting an error, "Undefined function 'popupmenu2WorkToDo' for input arguments of type 'struct'."
This is a good approach, one I often use myself. For example I might have a function called "SlidersMoved" and do a bunch of stuff in there for multiple controls. You have to write popupmenu2WorkToDo yourself and do whatever it is you want to do if your user interacts with any of your popups. You may not need to pass in anything except handles if you just want to get the current state of everything inside that function and take action accordingly. When you interact with a popup, or any control, the state of that control will already be the new state, not the prior one.
I did some reading on the error I was getting and now everything is work as it should. Thanks for the detailed explanations. This is my final code.
function popupmenu1_Callback(hObject, eventdata, handles)
popupmenu1 = get(handles.popupmenu1,'value');
switch popupmenu1
case 2
set(handles.popupmenu2,'value',popupmenu1);
handles=popup2WorkToDo(handles);
end
function popupmenu2_Callback(hObject, eventdata, handles)
handles=popupmenu2WorkToDo(handles);
function handles = popupmenu2WorkToDo(handles)
popupmenu2= get(handles.popupmenu2,'value');
switch popupmenu2
%do lots of stuff
Thank you.

Sign in to comment.

More Answers (0)

Categories

Find more on Software Development in Help Center and File Exchange

Asked:

on 10 Aug 2014

Edited:

on 27 Nov 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!