MATLAB GUI Error "Undefined function or variable 'val'"

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)

Risposte (1)

3 Commenti

You need to learn basics of GUI and callbacks. That link above is very usefull for that purpose. Be carefull with your words
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.

Accedi per commentare.

Categorie

Scopri di più su Interactive Control and Callbacks in Centro assistenza e File Exchange

Richiesto:

Rom
il 21 Ago 2013

Commentato:

il 12 Giu 2020

Community Treasure Hunt

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

Start Hunting!

Translated by