Get output values from addlistener
Show older comments
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;
addlistener(distpoly,'ROIMoved',@allevents);
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;
switch(evname)
case{'ROIMoved'}
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)]);
end
disp(xx); % it works OK
disp(yy); % it works OK
end
3 Comments
Adam Danz
on 15 Jan 2023
We can get this straightened out. To guide you in the best direction, it would be helpful to understand how you're going to use these values. Callback functions do not have outputs but it's easy to get the current position directly from the distpoly object. It's also fairly easy to store values to file or within an object property from within a callback. The best option depends on how you are going to use and access these values.
Costas Hourdakis
on 16 Jan 2023
Walter Roberson
on 16 Jan 2023
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.
Accepted Answer
More Answers (1)
Walter Roberson
on 16 Jan 2023
0 votes
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.
1 Comment
Costas Hourdakis
on 17 Jan 2023
Categories
Find more on Title 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!