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

6 visualizaciones (últimos 30 días)
Rom
Rom el 21 de Ag. de 2013
Comentada: Walter Roberson el 12 de Jun. de 2020
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)

Respuestas (1)

Walter Roberson
Walter Roberson el 21 de Ag. de 2013
  3 comentarios
Sugar Daddy
Sugar Daddy el 12 de Jun. de 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
Walter Roberson el 12 de Jun. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by