I am making a user interface with an input that represents an angle, to be set with uicontrol slider elements. It is expected that angles can wrap around the circle, so I would like that when the user increases the angle past the max value (2pi), the thumb and value would "increase" to zero (and similarly when the user decreases the angle past the min value (zero), the thumb and value would decrease to 2pi, thus behaving normally by wrapping around the interval.
function simplest_working_example
h1=figure();
init_phi1=45;
locinfo.phi1=uicontrol('Parent',h1,'style','slider','Min',0,'Max',360,'Value',init_phi1,'Position',[20 20 500 20],'Callback',{@update_fcn});
function update_fcn(~,~)
S1=[0 0];
l1=2;
E1=[l1*cos(locinfo.phi1.Value*2*pi/360) l1*sin(locinfo.phi1.Value*2*pi/360)];
plot([S1(1) E1(1)],[S1(2) E1(2)],'o-');hold on
xlim([-10 10]); ylim([-10 10]);axis square
hold off
end
end
I tried to edit the slider callback, but by construction it does not fire when the user clicks the up/down arrow and the slider is already at max/min (see this stackoverflow question). Are there any tricks for doing that, hopefully without the undocumented Java layer.
You can add the folloiwing at the beginning of update_fcn(~,~)
:
if locinfo.phi1.Value==360
locinfo.phi1.Value = 0;
elseif locinfo.phi1.Value==0
locinfo.phi1.Value = 360;
end
This produces the loop-back, but if you were continuously pressing the arrow, it requires you to click on it again.