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개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!