How to get the value of the edit textbox

33 views (last 30 days)
Bonny Okam
Bonny Okam on 10 Jul 2012
in my GUI, i have an Edit text-box where the users are expected to paste text and then click on a push button to perform a computation. i want to display an error message whenever a user clicks on the push button without pasting text into the textbox. Can anyone help me with suggestion on how to do this. Thanks in advance.
  1 Comment
Muhammad Adil Riaz
Muhammad Adil Riaz on 26 Mar 2017
You need to drag and drop textbox for showing error message and in push button function syntax code is set(handles.text1,'string','your error message'); it will show in your textbox.

Sign in to comment.

Answers (2)

Luffy
Luffy on 10 Jul 2012
Edited: Walter Roberson on 25 Dec 2012
Write below code in call-back of push-button
x = get(handles.edit1,'String'); %edit1 being Tag of ur edit box
if isempty(x)
fprintf('Error: Enter Text first\n');
else
% Write code for computation you want to do
end
  4 Comments
Walter Roberson
Walter Roberson on 25 Dec 2012
MATLAB does not use the error message "x is undeclared", at least not in any current version. Are you using a quite old version such as MATLAB 5 or R12 perhaps? Or are you trying to code this in Stateflow?
In Luffy's sample code, "x" is defined and given a value by the "x =" line that he showed.
Image Analyst
Image Analyst on 25 Dec 2012
But the code does not check to see if something has been pasted in, or check if what is in there is different than the last time they pushed the button, like the code I posted below does.

Sign in to comment.


Image Analyst
Image Analyst on 25 Dec 2012
I'm not sure that you can check for that. I don't think MATLAB will know whether text has been pasted into the text box, or if the user has typed something in. What you can do is to check if the text has changed since the button was last pushed. You can also check if the text box is empty, like Luffy showed you, but I don't think that is exactly what you asked. You didn't ask to check it is was empty. You want the user to paste in something and then click a push button and every time they push the button you said they were supposed to have pasted something in. In other words, whatever is there when they click the button, you have to make sure it has changed by the next time they push the button.
What I would do is to store the value of the edit box whenever they push the button. To do this you can either use persistent variables, global variables, or setappdata() and getappdata(). So the push button code would essentially do this (untested)
persistent textString;
% Get the current string in the text box.
currentString = get(handles.editText1, 'String');
% Compare the current string against the last string. Use strcmp().
% If the last string is different than the current string, you're okay.
% If the last string is the same as the current string (can either be something, or be empty or undefined) then warn user they have to change the edit text.
if isempty(textString)
% It's the first time through so textString will be empty.
% As long as currentText is not also empty, we're okay.
if ~isempty(currentString)
% Everything is fine.
% Reassign textString for next time
textString = currentString;
else
% It's the first time though, plus they have not entered anything yet.
uiwait(warndlg('You must enter new data into the text field'));
end
return;
end
if strcmp(textString, currentString)
% User changed text. All is right with the world.
% Reassign textString for next time
textString = currentString;
else
uiwait(warndlg('You must enter new data into the text field'));
end

Categories

Find more on Argument Definitions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!