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

3 Ansichten (letzte 30 Tage)
Stefano Roddaro
Stefano Roddaro am 9 Jan. 2019
Beantwortet: Jan am 9 Jan. 2019
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

Akzeptierte Antwort

Jan
Jan am 9 Jan. 2019
The code looks fine. I'd avoid gca, but use the 'Parent' property directly:
function move(src,event)
pt = get(src.Parent, 'CurrentPoint');

Weitere Antworten (0)

Kategorien

Mehr zu Creating, Deleting, and Querying Graphics Objects finden Sie in Help Center und File Exchange

Produkte


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by