MATLAB: How to create a popup mesage initially before program running

codeguiMATLABMATLAB Compiler

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?

Best Answer

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);