Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Need Help with Edit Text box in Matlab GUI ;D

Asked by jim on 1 Apr 2011

Hi, I've created a GUI using matlab and I have added a Text Box that a user can enter either numbers or text into.

I have made it so a user can enter a value into the text box, E.G '145' and that value will be assigned to a variable which i can the use this variable to do other things.

Also at the moment in the text box i have the text saying input a value. Is there any settings to make this writing disapear/ delete when the edit box is clicked on, because at the moment i have to manually delete this text before i can enter a number.

also is there a way to only let the user input values rather then letters? And also set a limit on what they can enter, eg only values between 20 to 100 will be accepted.

Edit Box Code Shown Below:

 function Text_Input_Height_Callback(hObject, eventdata, handles)
  % hObject    handle to Text_Input_Height (see GCBO)
  % eventdata  reserved - to be defined in a future version of MATLAB
  % handles    structure with handles and user data (see GUIDATA)

Text_Input_Height = get(hObject, 'string')

% --- Executes during object creation, after setting all properties.
function Text_Input_Height_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Text_Input_Height (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

0 Comments

jim

Products

No products are associated with this question.

2 Answers

Answer by Matt Fig on 1 Apr 2011
Accepted answer

This is a perennial problem with MATLAB GUIs. About the only way to come close without going into the Java is to have the string disappear when the mouse is over the editbox. Here is an example:

function [] = clear_edit_example()
% Show how to clear and editbox string when cursor is placed over it.
S.fh = figure('units','pixels',...
              'position',[500 500 300 50],...
              'menubar','none',...
              'numbertitle','off',...
              'name','clear_edit_example',...
              'resize','off');
S.ed = uicontrol('style','edit',...
                 'units','pixels',...
                 'position',[10 10 280 30],...
                 'fontsize',14,...
                 'string','Enter Value');
set(S.fh,'windowbuttonmotionfcn',{@fh_bmfcn,S})
function [] = fh_bmfcn(varargin)
% This is the WindowButtonMotionFcn for the figure.
S = varargin{3};  % Get handles structure.  Use GUIDATA or whatever.
CP = get(S.fh,'currentpoint'); % Get the current point in the fig.
EDP = get(S.ed,'position');  % The position of the editbox.
if CP(1)>EDP(1)&&CP(1)<(EDP(1)+EDP(3)) % Within X range
    if CP(2)>EDP(2)&&CP(2)< (EDP(2)+ EDP(4))  % Within Y range
        if strmatch(get(S.ed,'string'),'Enter Value')  % Starting string??
            set(S.ed,'string',[])
        end
    end
end

0 Comments

Matt Fig
Answer by david Landrith on 1 Apr 2011

use str2double(get(hObject, 'String')) to convert input strings to values

or try

get(hObject, 'Value')

1 Comment

jim on 1 Apr 2011

Hi, thanks for the answer. Yes Im using Text_Input_Height = get(hObject, 'string'). This gets the value the user inputs and assigns it to variable 'Text_Input_Height'

david Landrith

Contact us