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

7 ビュー (過去 30 日間)
Rom
Rom 2013 年 8 月 21 日
コメント済み: Walter Roberson 2020 年 6 月 12 日
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)

回答 (1 件)

Walter Roberson
Walter Roberson 2013 年 8 月 21 日
  3 件のコメント
Sugar Daddy
Sugar Daddy 2020 年 6 月 12 日
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 2020 年 6 月 12 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by