我在Matlab 2017b中尝试了以下代码:
function demo()
clc,close all
fig=figure();
ax=axes(fig,...
'Units','Normalized',...
'Position',[0.2,0.2,0.6,0.6],...
'XGrid','on',...
'YGrid','on')
slider=uicontrol(...
'Parent',fig,...
'Style','slider',...
'Units','normalized',...
'Position',[0.2,0.9,0.6,0.05],...
'Tag','slider1',...
'Min',0,...
'Max',10,...
'Value',1,...
'Callback',@slider_callback);
x=linspace(0,10);
y=1/3*x.^2;
plot(x,y,'b-')
grid on
xlabel('x-axis')
ylabel('y-axis')
hold on
p=plot(1,1/3,'ro')
hold off
function slider_callback(hObject,eventdata)
x=hObject.Value;
p.XData=x;
p.YData=1/3*x.^2;
drawnow
end
end
目前,我移动滑块,只有当我释放鼠标按钮时,才会更新该点。我可以进行哪些更改,以便在拖动滑块时看到点移动?
感谢。
更新:感谢VIG的评论,我能够做到这一点:
function demo()
clc,close all
fig=figure();
% initialize some parameters
xstart=4;
ystart=1/3*4^2;
ax=axes(fig,...
'Units','Normalized',...
'Position',[0.2,0.2,0.6,0.6],...
'XGrid','on',...
'YGrid','on');
slider=uicontrol(...
'Parent',fig,...
'Style','slider',...
'Units','normalized',...
'Position',[0.2,0.9,0.6,0.05],...
'Tag','slider1',...
'Min',0,...
'Max',10,...
'Value',xstart);
addlistener(slider,'Value',...
'PostSet',@(hObject, event) slider_callback(slider, event));
x=linspace(0,10);
y=1/3*x.^2;
plot(x,y,'b-')
grid on
xlabel('x-axis')
ylabel('y-axis')
hold on
plot([-5,10],[0,0],'k-');
plot([0,0],[-5,35],'k-');
p=plot(xstart,ystart,'ro');
a1=quiver(0,0,xstart,ystart,0,...
'LineWidth',1,...
'Color','red');
a2=quiver(0,0,0,xstart,0,...
'LineWidth',1,...
'Color','red');
a3=quiver(0,0,xstart,0,0,...
'LineWidth',1,...
'Color','red');
d=plot([0,xstart,xstart],[ystart,ystart,0],'k--');
hold off
function slider_callback(hObject,eventdata)
x=hObject.Value;
p.XData=x;
p.YData=1/3*x.^2;
a1.UData=x;
a1.VData=1/3*x.^2;
a2.VData=1/3*x.^2;
a3.UData=x;
d.XData=[0,x,x];
d.YData=[1/3*x.^2,1/3*x.^2,0];
drawnow
end
end
产生此图像:
我很想听到任何进一步的建议。
感谢。