Get output values from addlistener
15 views (last 30 days)
Show older comments
Costas Hourdakis on 15 Jan 2023
Commented: Costas Hourdakis on 18 Jan 2023
A rectangle is drawn in a figure and mooved to a new position. I use addlistener CallBackFunction (@allevents). I get the new (mooved) rectangle data (i.e. xx & yy) but only inside the CallBack function. I cannot extract them (i.e. the xx & yy as in the code below) from the CallBack function to my script for further use. I am new in MatLab; I have searched hours for a similar questions in the site. The answer in https://uk.mathworks.com/matlabcentral/answers/264979-continuous-slider-callback-how-to-get-value-from-addlistener does not seem to work for me. Could you please advise me? Many thanks. My code looks like:
f = figure;
hax = gca;
distpoly = drawrectangle(hax,'LineWidth',1,'Color', 'k');
pathcoord = distpoly.Position;
oldpos = xx; % Error: Unrecognized function or variable 'xx'.
newpos = yy; % Error: Unrecognized function or variable 'yy'.
function [xx, yy] = allevents(src,evt) % the [xx,yy] is not working as output
evname = evt.EventName;
xx = evt.PreviousPosition; % it works OK here but xx cannot be extracted from the CallBack function
yy = evt.CurrentPosition; % it works OK here but yy cannot be extracted from the CallBack function
disp(['ROI moved previous position: ' mat2str(evt.PreviousPosition)]);
disp(['ROI moved current position: ' mat2str(evt.CurrentPosition)]);
disp(xx); % it works OK
disp(yy); % it works OK
Some callback functions have outputs, but most do not. zoom / pan constraint callbacks have outputs, and data cursor mode UpdateFcn have callbacks, and a few others.
Adam Danz on 16 Jan 2023
Edited: Adam Danz on 18 Jan 2023
> The new ROI data would be used as "coordinates" to calculate other parameters of my code
There's no need for a listener to access the new ROI data. You can get that directly from the ROI object.
h = drawrectangle(hax, ...);
Rectangle with properties:
Position: [0.1438 0.43954 0.2533 0.47059]
Color: [0 0 0]
Parent: [1×1 Axes]
Show all properties
The h.Position property gives you the ROI's current position in the form of [x,y,w,h] where (x,y) is the lower left corner and (w,h) are the width and height which can be used to compute all 4 corners.
As for the additional resource request, MATLAB's documentation has always been more than sufficient for me. Other than that, I just search google when needed.
Show ROI Moving feedback
This demo uses a listener that responds to a MovingROI event. It update the text label showing the lower left coordinate of the rectangle and updates the xline/yline crosshairs.
fig = figure;
ax = gca;
h = drawrectangle(ax,'LineWidth',1,'Color', 'k');
txt = text(nan,nan,'','VerticalAlignment','top');
xl = xline(nan);
yl = yline(nan);
MovingROIFcn(h,,txt,xl,yl) % initialize the text obj and cross hairs
% Freeze axis limits
ax.XLimMode = 'manual';
ax.YLimMode = 'manual';
xy = src.Position(1:2);
txt.Position(1:2) = xy;
txt.String = sprintf('(%.3f, %.3f)', xy);
xl.Value = xy(1);
yl.Value = xy(2);
More Answers (1)
For most graphic objects and behaviors, you have code that configures a callback, and then the configuration code keeps running and returns when the configuration finishes. The configuration code does not (usually) wait for user interaction, just configures what will happen when the interaction is detected. When the interaction does happen and the callback is invoked, there is nothing to return any value to, as the configuration routine has already finished.
If you need the results of a callback then you need to invoke a function that uses uiwait() or waitfor() to configure the callback, and then sit around waiting for the callback to be invoked, after which you extract the results from whatever variables or objects, turn off the callback, and continue. See for example the code for inputdlg() which creates a figure and text edit area and waits for events, extracts the contents of the text edit object, destroys everything and returns.
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!