MATLAB simple slider on a figure

亡梦爱人 提交于 2019-12-05 22:44:42

Here is a code for a slider to plot corresponding column:

m = ones(5,1)*(1:5);
slmin = 1;
slmax = size(m,2);
plot(m(:,1))
hsl = uicontrol('Style','slider','Min',slmin,'Max',slmax,...
                'SliderStep',[1 1]./(slmax-slmin),'Value',1,...
                'Position',[20 20 200 20]);
set(hsl,'Callback',@(hObject,eventdata) plot(m(:,round(get(hObject,'Value')))) )

EDIT:

For better performance you can just update the YData values:

set(hsl,'Callback',@(hObject,eventdata) ...
    set(hline,'YData',m(:,round(get(hObject,'Value')))) )

To fix y axes limit, just set them manually with ylim([0 6]) after first plot call.

The code to create a slider is reasonable minimal:

uicontrol('Style', 'slider', 'Callback', @sliderCallback);

function sliderCallback(hObject, evt)
    fprintf('Slider value is: %d\n', get(hObject, 'Value') );
end

You will want to look at properties such as Position, to set the position on the figure, and Max and Min to set the possible range of values. Also note that if you do this inside a function, your sliderCallback can be a nested function which will probably make it easier to redraw your display. If you run this in a script, sliderCallback will have to be in a separate file.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!