How create cursor bar in UIAxes of App Designer?
조회 수: 26 (최근 30일)
이전 댓글 표시
Hi all,
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1118710/image.png)
But I could not use it in UIAxes. Do anyone know, please tell me.
Thank you
댓글 수: 0
답변 (1개)
chrisw23
2022년 9월 7일
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
chrisw23
2022년 9월 8일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!