Effacer les filtres
Effacer les filtres

Return values from figure callback

9 vues (au cours des 30 derniers jours)
Gerd Fuchs
Gerd Fuchs le 28 Nov 2022
Commenté : Stephen23 le 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?
  1 commentaire
Stephen23
Stephen23 le 28 Nov 2022
"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:

Connectez-vous pour commenter.

Réponses (1)

chrisw23
chrisw23 le 28 Nov 2022
Modifié(e) : chrisw23 le 28 Nov 2022
I suggest to use the lines UserData property to save your data.
hLines(k).UserData = ...

Catégories

En savoir plus sur Graphics Performance dans Help Center et File Exchange

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by