英文:
Matlab slider uicontrol: make a slider "loop back" for angles reaching 2pi or zero
问题
我正在制作一个用户界面,其中有一个输入表示一个角度,可以使用uicontrol滑块元素进行设置。预期角度可以绕过圆圈,因此当用户增加角度超过最大值(2π)时,我希望滑块的拇指和值会“增加”到零(当用户减小角度超过最小值(零)时,滑块的拇指和值会减少到2π,从而通过在区间周围进行包装来正常运行。
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
我尝试编辑滑块的回调函数,但由于构造的原因,当用户点击上/下箭头并且滑块已经处于最大/最小值时,它不会触发(请参阅这个stackoverflow问题)。是否有任何诀窍来做到这一点,最好是不使用未记录的Java层。
英文:
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.
答案1
得分: 1
你可以在 update_fcn(~,~)
的开头添加以下内容:
if locinfo.phi1.Value == 360
locinfo.phi1.Value = 0;
elseif locinfo.phi1.Value == 0
locinfo.phi1.Value = 360;
end
这会产生循环效果,但如果你一直按住箭头键,它需要你再次点击箭头键。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论