iptSetpointerbehavior issue in appdesigner

1 vue (au cours des 30 derniers jours)
Tugba Kircan
Tugba Kircan le 1 Juil 2022
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 commentaire
Walter Roberson
Walter Roberson le 1 Juil 2022
Do you have the Image Processing Toolbox installed?

Connectez-vous pour commenter.

Réponses (1)

Karan Singh
Karan Singh le 29 Sep 2023
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:
  1. 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.
  2. 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:
Hope this helps!
Karan Singh Khati

Catégories

En savoir plus sur Interactive Control and Callbacks dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by