Best practice - callback override / drag graphic elements...
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposta accettata
Jan
il 9 Gen 2019
The code looks fine. I'd avoid gca, but use the 'Parent' property directly:
function move(src,event)
pt = get(src.Parent, 'CurrentPoint');
0 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Creating, Deleting, and Querying Graphics Objects in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!