MATLAB GUI Error "Undefined function or variable 'val'"
1 view (last 30 days)
Show older comments
I made a GUI using GUIDE.
It contains a Pop-up menu containing 3 items (A,B,C) and a pushbutton.
This very simple Gui is designed to simply display a different text for whichever item the user chooses.
If I select the first value of my pop-up menu, I should get a return print of Hello. Sections of the code can be seen below. Why doesn't the code work?
M-FILE sections
function A_popup_Callback(hObject, eventdata, handles)
val = get(hObject,'Value');
switch val
case 1
x = 'Hello';
case 2
y = 'goodbye'
case 3
z = 'thank you'
end
function pushbutton1_Callback(hObject, eventdata, handles)
sprint('%s',x)
0 Comments
Answers (1)
Walter Roberson
on 21 Aug 2013
3 Comments
Sugar Daddy
on 12 Jun 2020
You need to learn basics of GUI and callbacks. That link above is very usefull for that purpose. Be carefull with your words
Walter Roberson
on 12 Jun 2020
Observe as I illustrate the technique of using the handles structure to share data between callbacks, as discussed at https://matlab.fandom.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F
function A_popup_Callback(hObject, eventdata, handles)
val = get(hObject,'Value');
switch val
case 1
handles.x = 'Hello';
case 2
handles.y = 'goodbye'
case 3
handles.z = 'thank you'
end
guidata(hObject, handles)
function pushbutton1_Callback(hObject, eventdata, handles)
sprint('%s',handles.x)
A careful reader might note that if val is not 1, then whatever is in handles.x is left unchanged: the user's code specifically asked to display x, not to display "whatever was assigned to in the switch statement". It would probably have made more sense if the user had assigned to x in all three cases, but they didn't.
See Also
Categories
Find more on Interactive Control and Callbacks in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!