Info

此问题已关闭。 请重新打开它进行编辑或回答。

I'm working on my VERY FIRST code ever. I'm attempting to make a simple GUI which asks for the users name, and verifies that it is correct. but I can't seem to use the input.

1 次查看(过去 30 天)
Here is my code:
function varargout = NameGame(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @NameGame_OpeningFcn, ...
'gui_OutputFcn', @NameGame_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
end
function NameGame_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
end
function name_input_Callback(hObject, eventdata, handles)
hObject
end
function submit_Callback(hObject, eventdata, handles)
name = 'Chris';
%Verify the response after you submit the name
choice = questdlg(['Are you sure your name is ', name, '?'], 'Yes', 'No');
%Verify Name input
switch choice
case 'Yes'
msgbox(['Welcome ',name,'!']);
case 'No'
msgbox(['Please resubmit your name']);
end
end
What the program does as of right now is that it opens up this window which asks the user to put their name:
Then, once you click submit, it opens up this popup window which asks you to confirm if this is really your name:
If you click "No", you are asked to re-input your name by this window message.
If you click "Yes", it welcomes you like this:
Now, there are a few issues with my program that I would like to get fixed:
  • As of right now, I don't know how to actually USE the name that the user inputs. I had to stick that line of code in there that defines "name = Chris", just so that I could get the other popup windows to work. **How can I get the input to be defined as the 'name' variable?
  • How can I get it so that the entire program closes once you confirm that your name is correct and click "Ok" [when it welcomes you](<http://i.imgur.com/n859t9O.jpeg)>?
  • Is there a way to get it to quit the entire program when you click "cancel" or the "x" button on the first window? As of right now, it simply moves onto the verification popup window if you try to cancel.
  • On a SUPER picky note... is there any way to get it so that when you start the program, you don't need to click on the text editor to begin typing in your name? (Meaning, it's already 'selected'?)
Sorry if this is a lot of quesitons... but I'd really appreciate any help! I'm really enjoying this learning process, but I do feel a bit lost with some of the "finer" aspects of coding...

回答(1 个)

Geoff Hayes
Geoff Hayes 2016-2-1
Chris - if name_input is the tag for your edit text box (where the user is supposed to type in his or her name), then in the submit button callback, you can access this text box using its handle from the handles structure (the third input to this function). For example,
function submit_Callback(hObject, eventdata, handles)
name = char(get(handles.name_input,'String'));
%Verify the response after you submit the name
% etc.
If you want the GUI to close once the user has pressed the OK button (after correctly entering the name), then use the handle of the message box and uiwait to wait for the message box to close before closing the main GUI. Something like
hMsgBox = msgbox(['Welcome ',name,'!'],'modal'); % create the message box
uiwait(hMsgBox); % block execution until msg box closes
close(handles.figure1); % close the main GUI
We use close to close the GUI using its handle figure1 (this may be different for you). You can use this function if the user presses the Cancel button or presses the x button to close the second dialog (hint look at the output questDlg when the user hits x). As for setting focus to your edit control, consider using uicontrol.
  2 个评论
Chris Gnam
Chris Gnam 2016-2-1
Thank you for your amazing response! I have two questions to follow up with that....
When you defined:
name = char(get(handles.name_input,'String'));
What is the "char(get" portion of that? I completely understand what the handles.name_input,'String' is doing... but why is the char(get infront of it?
Also, for your second thing... when you created the message box, what is the 'modal' you put in there? I do not have that in my code, and I'm curious what exactly it means...
Geoff Hayes
Geoff Hayes 2016-2-1
Chris - sometimes the line of code
get(handles.name_input,'String')
returns a cell array (to support multiline inputs) and so I use char to cast (or convert) the result to a character array.
I use modal along with the uiwait to prevent the user from returning to the GUI without first interacting with the message box. (See msgbox for details.)

此问题已关闭。

Community Treasure Hunt

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

Start Hunting!

Translated by