How to create a popup mesage initially before program running?

3 views (last 30 days)
slumberk
slumberk on 26 Feb 2011
Hye all.
I have a gui program. My question is how can i make my program started with user insert his name first. For example: when the user double click on my program, then there will be a popup message ask the user his name. After the user insert his name, then only the program will run. Can anyone help me on this?

Accepted Answer

Paulo Silva
Paulo Silva on 26 Feb 2011
Here's one way to do it, popupgui is the name of the GUI I used to create the code, this was created with the help of GUIDE, it will ask for the user name and if the user doesn't provide one or cancels the dialog the GUI will give an error and close it self, handles.UserName can be used in any other GUI function to do something with the name the user inserted, the name is just a string.
function popupgui_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to popupgui (see VARARGIN)
% Choose default command line output for popupgui
handles.output = hObject;
prompt = {'Please write your name'}; %what the user must do
dlg_title = 'User Login'; %the title of the dialog
num_lines = 1; %the number of lines that can be inserte
def = {''}; %this is the default name
UserName = inputdlg(prompt,dlg_title,num_lines,def); %the diaglog
if cellfun('isempty',UserName)
uiwait(errordlg('You must insert your name','Program terminated'))
error('You must insert your name')
end
if isempty(UserName)
uiwait(errordlg('You canceled the dialog','Program terminated'))
error('You canceled the dialog')
end
%convert cell to just a string and save it to the handles struct
handles.UserName=cell2mat(UserName); %so it can be used elsewhere
% Update handles structure
guidata(hObject, handles);
  2 Comments
Paulo Silva
Paulo Silva on 26 Feb 2011
I know that error, I couldn't get ride of him, it's no big deal because the program closes itself but if you really want to not have him force the user to write his name.
error=0;
while 1
prompt = {'Please write your name'}; %what the user must do
dlg_title = 'User Login'; %the title of the dialog
num_lines = 1; %the number of lines that can be inserted
def = {''}; %this is the default name
UserName = inputdlg(prompt,dlg_title,num_lines,def); %the diaglog
if cellfun('isempty',UserName)
uiwait(errordlg('You must insert your name','You must insert your name'))
error('You must insert your name')
error=1;
end
if isempty(UserName)
uiwait(errordlg('You canceled the dialog','Dialog canceled'))
error=1;
end
if ~error,break,end
end

Sign in to comment.

More Answers (1)

the cyclist
the cyclist on 26 Feb 2011
The "input" command will open a simple dialog box that can be used to prompt for the user's name.

Community Treasure Hunt

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

Start Hunting!