MATLAB: Saving the responses as struct from the GUI

guimatlab guisaving responses as struct

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

Best Answer

Ö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.
Related Question