Best practice - callback override / drag graphic elements...

2 ビュー (過去 30 日間)
Stefano Roddaro
Stefano Roddaro 2019 年 1 月 9 日
回答済み: Jan 2019 年 1 月 9 日
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

採用された回答

Jan
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 件)

カテゴリ

Help Center および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by