Error dialog box for push button when empty textbox; GUI

I have a GUI where the user would input their name, then press a button to proceed to the next GUI.
I want to make it so that if they leave the textbox blank and try to proceed by pressing the OK push button, they get an error dialog message prompting them to enter a name. It does not seem to work here. How would I accomplish this? Input is much appreciated.
The first line of the function is from the GUIDE output for .m file figure formation (as usual).
function pushbutton1_Callback(hObject, eventdata, handles)
editString = get(handles.edit1, 'String');
if editString == ''; % Enter in blank?
errordlg('Please enter a name into the text-box. We thank you in anticipation.','Error Code I');
else
gui2
close gui1
end

 Accepted Answer

use if isempty(editString )

7 Comments

Updated code like this thanks to your suggestion:
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if isempty(editString)
errordlg('Please enter a name into the text-box. We thank you in anticipation.','Error Code I');
else
gui2
close gui1
end
But I am getting an error like this:
Undefined function or variable 'editString'.
Error in gui1>pushbutton1_Callback (line 107)
if isempty(editString)
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in gui1 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)gui1('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
You still need your:
editString = get(handles.edit1, 'String');
line above all that, otherwise editString is undefined as the error mentions.
Yes it is above that because I put it (editString = get(handles.edit1, 'String');) under this function:
function edit1_Callback(hObject, eventdata, handles)
And this edit1_Callback function is above this function:
function pushbutton1_Callback(hObject, eventdata, handles)
However, the error persists...
Callbacks have their own workspace, the workspace does not follow through to functions below so you still need it as you had it in your original post with
function pushbutton1_Callback(hObject, eventdata, handles)
editString = get(handles.edit1, 'String');
...
This is the callback for the pushbutton. It knows nothing about the edit control or the editString that is in its callback. All it knows is that there is a handles structure and that you can get your edit box from that and again extract the string into the workspace of the pushbutton.
Thank you very much Adam for this pedagogical answer.
My apologies, I meant to accept your answer (Adam) but it did not give me an option. However, I appreciate the input from both Stalin and Adam.
I added mine to stalin samuel's answer since it was just an extension of his correct input.

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Asked:

on 15 Dec 2014

Commented:

on 15 Dec 2014

Community Treasure Hunt

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

Start Hunting!