问题
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