Clear Filters
Clear Filters

Return of handle, which was selected with 'ButtonDownFcn'

1 view (last 30 days)
Hey there,
I plotted some data in just one figure, after that, I want to select some plots by mouse in the figure and I want to highlight it by increasing the 'LineWidth' and get the information, which of the plotted object has been selected.
In my main script, I use the following code to call the function (dxf.handle contains all the handles, which have been plotted before):
selected_entities = sub_select(dxf.handle);
Unable to resolve the name 'dxf.handle'.
The function has the following code:
function selected_object = sub_select(H)
set(H, 'ButtonDownFcn', selected_object = @LineSelected)
end
function ObjectH = LineSelected(ObjectH, EventData)
set(ObjectH, 'LineWidth', 2.5);
end
In general the code works and it's possible to highlight the objects, but I didn't get the information, which handle was selected. This information should be stored in selected_entities. At the moment, I just get this:
selected_object = @LineSelected
Thanks in advance for your help.
Best regards
Daniel

Accepted Answer

Walter Roberson
Walter Roberson on 27 Jun 2024
function selected_object = sub_select(H)
set(H, 'ButtonDownFcn', selected_object = @LineSelected)
end
That code attempts to call
set(H, 'ButtonDownFcn', 'selected_object', '@LineSelected')
which is going to fail because '@LineSelected' is not a name-value pair.
If you were to use
function selected_object = sub_select(H)
set(H, 'ButtonDownFcn', 'selected_object = @LineSelected')
end
then that might not be rejected immediately, but it also would not work: text entries for functions are evaluated in the base workspace, so selected_object would be set in the base workspace, where it would not be available to be returned by the sub_select function.
You have the further issue that you expect sub_select to return the selected object, but sub_select is instead just setting the button down fnc callback (which will be invoked at some later point.)
You would have to do something like have sub_select set the button down function, and then uiwait or waitfor some property to be changed, and then pull out the value of that property.
  2 Comments
Daniel Ludwig
Daniel Ludwig on 28 Jun 2024
Hi Walter,
thanks for your reply, I unterstand.
Do you have any idea, how to "pull out the value"?
Walter Roberson
Walter Roberson on 28 Jun 2024
Edited: Walter Roberson on 28 Jun 2024
function selected_object = sub_select(H)
ax = ancestor(H(1), 'axes');
ax.UserData = [];
set(H, 'ButtonDownFcn', @(src,~) set(ax.UserData, src));
waitfor(ax, 'UserData');
selected_object = ax.UserData;
set(H, 'ButtonDownFcn', '')
end

Sign in to comment.

More Answers (0)

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!