Trying to add callback function in class

6 views (last 30 days)
I am attempting to create a class the will have two line ROI objects. This class should have a callback function for when the lines are moved by the user. I am able to add a listener to the lines but only if the callback function is static.
Is it possible to do this without using a static method? Ultimatly I would like to use the callback method to compare the positions of the two lines as one of them is moved.
classdef imageRange < handle
properties
region double
line1
line2
rect
X double
end
methods
function obj = imageRange(axes, dataAxes)
x = dataAxes{1,4};
y = dataAxes{2,4};
midpoint = mean([x(1),x(3)]);
obj.X = floor(midpoint);
obj.rect = rectangle(axes,'Position',[obj.X,y(1),...
ceil(midpoint)-floor(midpoint), y(3) - y(1)]);
obj.rect.EdgeColor = [0,0,0,0];
obj.rect.FaceColor = [.5,.5,.5];
obj.rect.FaceAlpha = .1;
obj.line1 = drawline(axes,'Position',[floor(midpoint), y(1); ...
floor(midpoint), y(3)],'Color',[0.6350 0.0780 0.1840], ...
'InteractionsAllowed','translate', ...
'MarkerSize',.1, ...
'LineWidth',1);
addlistener(obj.line1, 'MovingROI', @(src,event)obj.allevents(src,event));
obj.line2 = drawline(axes,'Position',[ceil(midpoint), y(1); ...
ceil(midpoint), y(3)],'Color',[0 0.4470 0.7410], ...
'InteractionsAllowed','translate', ...
'MarkerSize',.1, ...
'LineWidth',1);
obj.region = dataAxes{1,5};
end
end
methods (Static)
function allevents(src,event)
event.PreviousPosition(1,1)
end
end
end

Accepted Answer

Manikanta Aditya
Manikanta Aditya on 12 Apr 2025
Yes, you can achieve this without using a static method. You can define the callback function as a regular method within your class and use an anonymous function to pass the object instance to the callback. Here's how you can modify your class:
classdef imageRange < handle
properties
region double
line1
line2
rect
X double
end
methods
function obj = imageRange(axes, dataAxes)
x = dataAxes{1,4};
y = dataAxes{2,4};
midpoint = mean([x(1),x(3)]);
obj.X = floor(midpoint);
obj.rect = rectangle(axes,'Position',[obj.X,y(1),...
ceil(midpoint)-floor(midpoint), y(3) - y(1)]);
obj.rect.EdgeColor = [0,0,0,0];
obj.rect.FaceColor = [.5,.5,.5];
obj.rect.FaceAlpha = .1;
obj.line1 = drawline(axes,'Position',[floor(midpoint), y(1); ...
floor(midpoint), y(3)],'Color',[0.6350 0.0780 0.1840], ...
'InteractionsAllowed','translate', ...
'MarkerSize',.1, ...
'LineWidth',1);
addlistener(obj.line1, 'MovingROI', @(src,event)obj.allevents(src,event));
obj.line2 = drawline(axes,'Position',[ceil(midpoint), y(1); ...
ceil(midpoint), y(3)],'Color',[0 0.4470 0.7410], ...
'InteractionsAllowed','translate', ...
'MarkerSize',.1, ...
'LineWidth',1);
addlistener(obj.line2, 'MovingROI', @(src,event)obj.allevents(src,event));
obj.region = dataAxes{1,5};
end
function allevents(obj, src, event)
% Callback function to handle the event
disp('Line moved');
disp(['Previous Position: ', num2str(event.PreviousPosition)]);
disp(['Current Position: ', num2str(event.CurrentPosition)]);
% Compare positions of the two lines
pos1 = obj.line1.Position;
pos2 = obj.line2.Position;
disp(['Line 1 Position: ', num2str(pos1)]);
disp(['Line 2 Position: ', num2str(pos2)]);
end
end
end
In this updated version, the allevents method is a regular method of the class, and the listener is set up using an anonymous function that passes the object instance (obj) to the callback. This way, you can access the properties and methods of the class within the callback function.

More Answers (0)

Categories

Find more on Object Containers in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!