How to get the value of the edit textbox

조회 수: 17 (최근 30일)
Bonny Okam
Bonny Okam 2012년 7월 10일
댓글: Muhammad Adil Riaz 2017년 3월 26일
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
Muhammad Adil Riaz
Muhammad Adil Riaz 2017년 3월 26일
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.

댓글을 달려면 로그인하십시오.

답변 (2개)

Luffy
Luffy 2012년 7월 10일
편집: Walter Roberson 2012년 12월 25일
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
Walter Roberson
Walter Roberson 2012년 12월 25일
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 2012년 12월 25일
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.

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2012년 12월 25일
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

카테고리

Help CenterFile Exchange에서 Function Creation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by