Return values from figure callback
8 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Stephen23
2022 年 11 月 28 日
"How can I return the values from the callback? "
Callbacks don't return arguments.
The standard ways of passing data around a GUI are explained here:
参考
カテゴリ
Help Center および File Exchange で Interactive Control and Callbacks についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!