Return values from figure callback

12 次查看(过去 30 天)
Gerd Fuchs
Gerd Fuchs 2022-11-28
评论: Stephen23 2022-11-28
Hello,
I want to interactively choos datapoints from a graphic. The idea is to click on two datapoints which mark the start and the end of the region of interest. I came across this answer which was very helpful. Nevertheless, I'd like to implement this functionality in a function. How can I return the values from the callback?
In the main file, I'm generating the plot. Furhtermore, I'm calling the function to choose datapoints from the plot:
hFig = figure
plot(xx, yy);
xy = fchoose_from_plot(hFig) % call the function to choose datapoints from plot
The function looks as follows
function [xy] = fchoose_from_plot(hFig)
% Start brushing mode and specify callback for when complete
hBr = brush(hFig);
hBr.ActionPostCallback = {@fonBrushAction,{'data'}};
brush on
% save selected datapoints
xy = [];
fprintf('select first ...\n');
uiwait;
xy = [xy, data'];
fprintf('select second ...\n');
uiwait;
xy = [xy, data'];
% turn off the brush and close figure
hBr.Enable = 'off';
close(hFig)
end
And the callback function is basically the same as in the linked post but I've tried to change 'base' to 'caller' in assignin.
% Callback for after the brush action has been completed
function fonBrushAction(~,eventdata,names)
% Extract plotted graphics objects
% Invert order because "Children" property is in reversed plotting order
hLines = flipud(eventdata.Axes.Children);
% Loop through each graphics object
for k = 1:numel(hLines)
% Check that the property is valid for that type of object
% Also check if any points in that object are selected
if isprop(hLines(k),'BrushData') && any(hLines(k).BrushData)
% Output the selected data to the base workspace with assigned name
ptsSelected = logical(hLines(k).BrushData.');
data = [hLines(k).XData(ptsSelected).' ...
hLines(k).YData(ptsSelected).'];
% assignin('base',names{k},data)
assignin('caller',names{k},data)
end
uiresume;
end
end
Nevertheless, I get the error Unrecognized function or variable 'data' from where I can conclude, that the datapoints are not returned properly in the workspace of the function. How can I solve this issue?

回答(1 个)

chrisw23
chrisw23 2022-11-28
编辑:chrisw23 2022-11-28
I suggest to use the lines UserData property to save your data.
hLines(k).UserData = ...

类别

Help CenterFile Exchange 中查找有关 Graphics Performance 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by