Need Help with Edit Text box in Matlab GUI ;D

1 次查看(过去 30 天)
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

采纳的回答

Matt Fig
Matt Fig 2011-4-1
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

更多回答(1 个)

david Landrith
david Landrith 2011-4-1
use str2double(get(hObject, 'String')) to convert input strings to values
or try
get(hObject, 'Value')
  1 个评论
jim
jim 2011-4-1
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'

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by