I supposed to assign the entered strings or chosen values to a struct in the callback functions. I have something like below but I cannot embed it into function. In command window, matlab returns me some numerical values rather than responses
function b
d = dialog('Position',[300 300 300 300],'Name','My Dialog');
Name = uicontrol('Parent',d,...
'Style','text',...
'Position',[20 180 50 100 ],...
'String','Name');
NameAns = uicontrol('Parent', d,...
'Style', 'edit',...
'String','Type Your Name',...
'position', [70 260 100 20] )
Gender = uicontrol('Parent',d,...
'Style','text',...
'Position',[20 150 50 100 ],...
'String','Gender');
GenderAns = uicontrol('Parent', d,...
'Style', 'popup', 'String', {'female','male', 'other'},...
'Position', [70 230 90 20 ])
Color = uicontrol('Parent',d,...
'Style','text',...
'Position',[20 120 50 100 ],...
'String','Favorite Color');
ColorAns = uicontrol('Parent', d,...
'Style', 'popup', 'String', {'Blue','Red', 'Orange'},...
'Position', [70 200 90 20 ])
uicontrol('Parent', d, 'Style', 'pushbutton',...
'String', 'OK',...
'Position', [90, 90, 110, 30],...
'FontSize',18,'Callback',@OKCallBack);
function ConfCallBack(source,eventdata)
disp(get(source,'Value'));
subject.name = NameAns
subject.gender = GenderAns
subject.color = ColorAns
uiwait (d)
end
function OKCallBack(source,eventdata)
close(d)
end
end

 채택된 답변

Geoff Hayes
Geoff Hayes 2016년 12월 30일

0 개 추천

Özge - the "answers" like NameAns, GenderAns, ColorAns are handles to the controls that you have created. That is why they are numeric and do not correspond to the values that you have entered (or choices that you have made). You will need to use these handles to get the values for the name, gender, and favourite colour which you will use in the OK button callback. Try the following
function b
d = dialog('Position',[300 300 300 300],'Name','My Dialog');
Name = uicontrol('Parent',d,...
'Style','text',...
'Position',[20 180 50 100 ],...
'String','Name');
hName = uicontrol('Parent', d,...
'Style', 'edit',...
'String','Type Your Name',...
'position', [70 260 100 20] );
Gender = uicontrol('Parent',d,...
'Style','text',...
'Position',[20 150 50 100 ],...
'String','Gender');
hGender = uicontrol('Parent', d,...
'Style', 'popup', 'String', {'female','male', 'other'},...
'Position', [70 230 90 20 ]);
Color = uicontrol('Parent',d,...
'Style','text',...
'Position',[20 120 50 100 ],...
'String','Favorite Color');
hColour = uicontrol('Parent', d,...
'Style', 'popup', 'String', {'Blue','Red', 'Orange'},...
'Position', [70 200 90 20 ]);
uicontrol('Parent', d, 'Style', 'pushbutton',...
'String', 'OK',...
'Position', [90, 90, 110, 30],...
'FontSize',18,'Callback',@OKCallBack);
subject = [];
function OKCallBack(source,eventdata)
subject.name = get(hName,'String');
genderList = get(hGender,'String');
subject.gender = genderList{get(hGender,'Value')};
colourList = get(hColour,'String');
subject.color = colourList{get(hColour,'Value')};
close(d)
end
end
Note how subject is declared within the body of b and is updated in the OKCallack. How you return that to your calling function is dependent upon how b is used within the larger program.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

질문:

2016년 12월 22일

답변:

2016년 12월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by