MATLAB GUI Error "Undefined function or variable 'val'"
조회 수: 6 (최근 30일)
이전 댓글 표시
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
답변 (1개)
Walter Roberson
2013년 8월 21일
댓글 수: 3
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
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 Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!