方法1:如果你是用GUIDE创建的界面,新建一个空白窗口,在窗口里放置一个style属性为edit的文本框,其默认tag值为edit1。再放置一个style值为slider的滑块,其默认tag值为slider1。
edit1的callback函数中添加如下语句:
set(handles.slider1,'value',str2num(get(hObject,'string')));
slider1的callback函数中添加如下语句:
set(handles.edit1,'string',num2str(get(hObject,'value')));
方法2:如果你想直接用代码创建界面,则新建一个m文件,输入如下代码:
function slider_text
MainFigure=figure('menubar','none','numbertitle','off','position',[100 100 250 200],'name','滑块和文本框');
text1=uicontrol('style','edit','parent',MainFigure,'position',[50 150 150 20],'string','请输入一个0到1之间的数','callback',@text1_callback);
slider1=uicontrol('style','slider','parent',MainFigure,'position',[50 100 150 20],'callback',@slider1_callback);
function slider1_callback(hObject,event)
set(text1,'string',num2str(get(hObject,'value')))
end
function text1_callback(hObject,event)
set(slider1,'value',str2num(get(hObject,'string')))
end
end