matlab中滑块咋么用,在MATLAB中使用连续滑块的值

像这样的东西?

function sliderDemo

f = figure(1);

%// Some simple to plot function (with tuneable parameter)

x = 0:0.1:2*pi;

y = @(A) A*sin(x);

%// Make initial plot

A = 1;

p = plot(x, y(A));

axis tight

axis([0 2*pi -10 10])

%// re-position the axes to make room for the slider

set(gca, 'position', [0.1 0.25 0.85 0.7]);

%// initialize the slider

h = uicontrol(...

'parent' , f,...

'units' , 'normalized',... %// so yo don't have to f*ck with pixels

'style' , 'slider',...

'position', [0.05 0.05 0.9 0.05],...

'min' , 1,... %// Make the A between 1...

'max' , 10,... %// and 10, with initial value

'value' , A,... %// as set above.

'callback', @sliderCallback); %// This is called when using the arrows

%// and/or when clicking the slider bar

%// THE MAGIC INGREDIENT

%// ===========================

hLstn = handle.listener(h,'ActionEvent',@sliderCallback); %#ok

%// (variable appears unused, but not assigning it to anything means that

%// the listener is stored in the 'ans' variable. If "ans" is overwritten,

%// the listener goes out of scope and is thus destroyed, and thus, it no

%// longer works.

%// ===========================

%// The slider's callback:

%// 1) clears the old plot

%// 2) computes new values using the (continuously) updated slider values

%// 3) re-draw the plot and re-set the axes settings

function sliderCallback(~,~)

delete(p);

p = plot(x, y(get(h,'value')));

axis tight

axis([0 2*pi -10 10])

end

end

PS – 你找不到它并不奇怪 – 它没有记录.我知道这是从Yair Altman’s site.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MATLAB滑块是一种常用的交互式工具,用于调整参数或浏览数据。下面是使用MATLAB滑块的简单步骤: 1. 创建一个滑块对象:使用uicontrol函数创建一个滑块对象。例如,可以使用以下代码创建一个滑块对象: ``` slider = uicontrol('Style','slider',... 'Min',0,'Max',10,'Value',5,... 'Position',[200 150 200 20]); ``` 这将创建一个滑块对象,最小为0,最大为10,初始为5,位置为[200,150],大小为[200,20]。 2. 指定回调函数:当用户移动滑块时,需要执行一些操作。为此,需要指定一个回调函数。例如,可以使用以下代码指定一个回调函数: ``` set(slider,'Callback',@slider_callback); ``` 其slider_callback是一个自定义的回调函数。 3. 实现回调函数:在回调函数,可以获取滑块并执行相应的操作。例如,以下代码是一个简单的回调函数,用于在每次滑块移动时更新一个文本框的: ``` function slider_callback(hObject,eventdata) val = get(hObject,'Value'); set(handles.textbox,'String',num2str(val)); end ``` 其,hObject是滑块对象,eventdata是事件数据。get函数用于获取滑块的当前,set函数用于将该更新到文本框。 4. 运行程序:运行MATLAB程序时,会显示滑块对象和文本框。当用户移动滑块时,会触发回调函数,并更新文本框的。 注意事项: - 滑块对象必须与回调函数在同一个作用域内。 - 回调函数的handles变量必须包含文本框对象的句柄。可以在程序的开头使用以下代码创建文本框对象: ``` handles.textbox = uicontrol('Style','text',... 'Position',[200 100 200 20]); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值