Value of Pop-up Menu

I want to get value as a number from pop-menu. for example if user want to select gender. 1 for male and 2 for female. i have create this code
function gender_Callback(hObject, eventdata, handles)
gender = (get(hObject, 'Value'));
switch gender
case 'Male'
gender = 1;
case 'Female'
gender = 2;
end
% Save the new n value
handles.gender = gender;
guidata(hObject,handles)
and this for get value
gender = handles.gender;
but it's result not 1 or 2, but strange number (e.g 212.0060). what's wrong and how the correct code. thank you very much

 채택된 답변

Image Analyst
Image Analyst 2013년 5월 13일

1 개 추천

get() does not return the word in the popup list, it returns the index. So the whole switch statement is not even needed. Actually the function is not even needed so I'm not sure why you're using it. Anytime, in any function, that you need the gender number, simply do this
gender = get(handles.gender, 'value');
No switch needed, no code in the popup's callback needed, all that is needed is the line above. Use it anywhere that you were incorrectly going to use gender=handles.gender. That code was incorrect because that was giving you the handle (a floating point number) rather than the number you thought.

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 5월 13일

0 개 추천

The code should be
gender = (get(hObject, 'Value'));
switch gender
case 1
%do
case 2
%do
end

댓글 수: 1

Retno
Retno 2013년 5월 13일
I try that code and it doesn't work mister

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2013년 5월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by