Best practice - callback override / drag graphic elements...
2 次查看(过去 30 天)
显示 更早的评论
Hi,
I am writing a set of short and self-contained demo scripts for didactic purposes and I need to code in some user interaction. I looked a bit around for ideas and I ended up with using callback overrides, nested functions and "shared" variables (not sure how to call them sorry, they turn cyan like globals... hf1 for instance in the example below). Is this kind of code design "ok"? Or would you rather suggest different/better ways to organize things? Is there any official documentation on all this (callback override examples, draggable graphic elements etc)? I just found here and there few sporadic user examples.
thanx! S
% Simple user interaction
%
function Simple()
hf1 = figure(1);
clf;
hf1.WindowButtonUpFcn = @stopdrag;
xv = 2*pi*(0:0.01:1);
yv = sin(xv);
hpl = plot(xv,yv,'k','LineWidth',1); hold on;
hln = line([pi pi],[-1.2,+1.2]);
hpt = plot(pi,0,'o');
xlabel('x');
ylabel('sin(x)');
axis([0 2*pi -1.2 1.2]);
hln.ButtonDownFcn = @startdrag;
% events
function startdrag(src,event)
hf1.WindowButtonMotionFcn = @move;
end
function move(src,event)
pt = get(gca, 'CurrentPoint');
hln.XData = [1 1]*pt(1,1);
hpt.XData = pt(1,1);
hpt.YData = sin(pt(1,1));
end
function stopdrag(src,event)
hf1.WindowButtonMotionFcn = [];
end
end
0 个评论
采纳的回答
Jan
2019-1-9
The code looks fine. I'd avoid gca, but use the 'Parent' property directly:
function move(src,event)
pt = get(src.Parent, 'CurrentPoint');
0 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!