How create cursor bar in UIAxes of App Designer?
27 views (last 30 days)
Show older comments
Hi all,
But I could not use it in UIAxes. Do anyone know, please tell me.
Thank you
0 Comments
Answers (1)
chrisw23
on 7 Sep 2022
classdef Cursor < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
figCursorExample matlab.ui.Figure
axCursor matlab.ui.control.UIAxes
end
properties (Access = private)
cursorLine
sinLine
lockCursorLine
intersecPoint
end
methods (Access = private)
function MouseMoveCallback(app,~,~)
% check if mouse pointer within axes area
x2 = app.axCursor.CurrentPoint(1,1);
y2 = app.axCursor.CurrentPoint(1,2);
x2min = min(app.axCursor.XLim);
x2max = max(app.axCursor.XLim);
y2min = min(app.axCursor.YLim);
y2max = max(app.axCursor.YLim);
if (x2 >= x2min && x2 <= x2max) && (y2 >= y2min && y2 <= y2max)
if app.lockCursorLine
app.cursorLine.XData = [x2 x2];
end
end
end
function ReleaseLineLockCallback(app,~,~)
if app.lockCursorLine
x = app.cursorLine.XData(1);
app.intersecPoint.Position(1:2) = [x,sin(x)];
end
app.lockCursorLine = false;
end
function GrabCursorCallback(app,~,~)
app.lockCursorLine = true;
end
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
app.axCursor.XLim = [0 2*pi];
app.axCursor.YLim = [-2 2];
x = [0:0.1:2*pi];
y = sin(x);
app.sinLine = plot(app.axCursor,x,y);
app.cursorLine = line(app.axCursor,[pi pi],[-2 2]);
dataCursorManager = datacursormode(app.figCursorExample);
app.intersecPoint = dataCursorManager.createDatatip(app.sinLine);
app.intersecPoint.Tag = "intersecPoint";
app.intersecPoint.DataTipStyle = 'MarkerOnly';
drawnow
app.intersecPoint.Position = [app.cursorLine.XData(1) sin(app.cursorLine.XData(1)) 0];
app.figCursorExample.WindowButtonMotionFcn = @app.MouseMoveCallback;
app.figCursorExample.WindowButtonUpFcn = @app.ReleaseLineLockCallback;
app.cursorLine.ButtonDownFcn = @app.GrabCursorCallback;
end
end
% Component initialization
...
% App creation and deletion
...
This is just an example and adapted to a sine wfm but maybe it helps to figure out how it could work for you.
Here you have to hit the cursor line exactly to grab it for moving, but you can use markers too.
3 Comments
chrisw23
on 8 Sep 2022
Sry I can't rewrite your code, but some hints.
- There's a large amount of events to process while moving, so avoid to redraw everthing. Don't clear your axes. Don't plot new lines when you just want to update the XData/YData. Don't overwrite variables when they don't change. There's a lot of code that belongs to the startup function and not to the callback. Event then there will be a delayed redraw. I prefer the dedicated line click action, because it will not slow down my code when I just move the mouse pointer over the axis area. But this is your code, your choice.
Best regards
Christian
See Also
Categories
Find more on Interactive Control and Callbacks in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!