iptSetpointerbehavior issue in appdesigner
1 次查看(过去 30 天)
显示 更早的评论
Hello everyone,
İ am trying to record the position of the cursor and run a motor when is inside a specific area. İ am encountering an issue thats :
''Undefined function 'iptSetPointerBehavior' for input arguments of type 'matlab.graphics.primitive.Patch'.''
Below is my code:
properties (Access = public)
arduinoObj % Description
message
region
end
properties (Access = public)
T % Description
end
properties (Access = public)
region1
end
properties (Access = public)
amplitude % Description
end
methods (Access = private)
function mycallback(app,src,event)
display(event.SelectedOption);
end
end
function startupFcn(app)
% Plot patch on uiaxes
hold(app.UIAxes, 'on')
app.region1 = patch(app.UIAxes,[5 5 10 10],[5 5 5 0],'r','FaceAlpha',1,...
'LineWidth',2,'LineStyle','-','tag','region1');
% Define pointer behavior for patch
pm.enterFcn = @(~,~) cursorPositionFeedback(app, app.region1, 'in');
pm.exitFcn = @(~,~) cursorPositionFeedback(app, app.region1, 'out');
pm.traverseFcn = [];
iptSetPointerBehavior(app.region1, pm)
% Enable pointer manager for app
iptPointerManager(app.UIFigure,'enable');
function cursorPositionFeedback(app, hobj, inout)
% When inout is 'in', change hobj facecolor to red and update textbox.
% When inout is 'out' change hobj facecolor to blue, and clear textbox.
% Check tag property of hobj to identify the object.
switch lower(inout)
case 'in'
facecolor = 'g';
txt = 'Inside region1';
pointer = 'fleur';
%Writeline(app.arduinoObj,...)
case 'out'
facecolor = 'r';
txt = '';
pointer = 'crosshair';
%Writeline(app.arduinoObj,...)
end
hobj.FaceColor = facecolor;
app.TextAreaEditField = txt;
set(app.UIFigure, 'Pointer', pointer)
end
end
% Window button motion function: UIFigure
function UIFigureWindowButtonMotion(app, event)
% detect if the mouse is within the axes
cp = app.UIFigure.CurrentPoint;
isInAxes = cp(1) >= app.UIAxes.Position(1) &&...
cp(1) <= sum(app.UIAxes.Position([1,3])) &&...
cp(2) >= app.UIAxes.Position(2) &&...
cp(2) <= sum(app.UIAxes.Position([2,4]));
if isInAxes
set(app.CurrentPositionEditField, 'Value',...
sprintf('%.2f, %.2f', app.UIAxes.CurrentPoint(1,1:2)))
else
set(app.CurretPositionEditField, 'Value', '')
end
回答(1 个)
Karan Singh
2023-9-29
Hi Tugba,
From what I understand, you are trying to create a graphical user interface (GUI) in MATLAB that includes a plot and a patch object. You want to track the position of the cursor within the plot and perform certain actions when the cursor is inside or outside a specific region defined by the patch object.
However, you are encountering an error message stating that the “iptSetPointerBehavior” function is undefined for the input arguments of type “matlab.graphics.primitive.Patch”. This error suggests that the function is not recognized by MATLAB.
To resolve this issue, you have a few options:
- Install the Image Processing Toolbox: If you have a license for the Image Processing Toolbox, you can install it from the MATLAB Add-Ons menu.
- Use an alternative method: You can use the “WindowButtonMotionFcn” callback to track the cursor position and update the behavior accordingly.
Here's an example of how you can modify your code to achieve cursor position feedback:
% Window button motion function: UIFigure
function UIFigureWindowButtonMotion(app, event)
% detect if the mouse is within the axes
cp = app.UIFigure.CurrentPoint;
isInAxes = cp(1) >= app.UIAxes.Position(1) &&...
cp(1) <= sum(app.UIAxes.Position([1,3])) &&...
cp(2) >= app.UIAxes.Position(2) &&...
cp(2) <= sum(app.UIAxes.Position([2,4]));
if isInAxes
set(app.CurrentPositionEditField, 'Value',...
sprintf('%.2f, %.2f', app.UIAxes.CurrentPoint(1,1:2)))
cursorPositionFeedback(app, 'in');
else
set(app.CurretPositionEditField, 'Value', '')
cursorPositionFeedback(app, 'out');
end
end
function cursorPositionFeedback(app, inout)
% When inout is 'in', change region1 facecolor to red and update textbox.
% When inout is 'out' change region1 facecolor to blue, and clear textbox.
% Check tag property of region1 to identify the object.
switch lower(inout)
case 'in'
facecolor = 'g';
txt = 'Inside region1';
pointer = 'fleur';
%Writeline(app.arduinoObj,...)
case 'out'
facecolor = 'r';
txt = '';
pointer = 'crosshair';
%Writeline(app.arduinoObj,...)
end
app.region1.FaceColor = facecolor;
app.TextAreaEditField = txt;
set(app.UIFigure, 'Pointer', pointer)
end
In this modified code, the “WindowButtonMotionFcn” callback function tracks the cursor position and calls the “cursorPositionFeedback” function to update the behaviour accordingly.
Attached below are some documentation links that you may find helpful:
- Graphics - MATLAB & Simulink (mathworks.com)
- UI axes appearance and behavior - MATLAB (mathworks.com)
- Develop Apps Using App Designer - MATLAB & Simulink (mathworks.com)
Hope this helps!
Karan Singh Khati
0 个评论
另请参阅
类别
在 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!