MATLAB simple slider on a figure

和自甴很熟 提交于 2019-12-22 10:58:30

问题


I have a matrix to be plotted one column at a time. Is it possible to add a slider to a MATLAB figure (without heavy GUI programming) so that by moving the slider, different columns are shown in the current axis?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/15975758/matlab-simple-slider-on-a-figure

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