Return values from figure callback

14 visualizzazioni (ultimi 30 giorni)
Gerd Fuchs
Gerd Fuchs il 28 Nov 2022
Commentato: Stephen23 il 28 Nov 2022
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?

Risposte (1)

chrisw23
chrisw23 il 28 Nov 2022
Modificato: chrisw23 il 28 Nov 2022
I suggest to use the lines UserData property to save your data.
hLines(k).UserData = ...

Categorie

Scopri di più su Interactive Control and Callbacks in Help Center e File Exchange

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by