Return of handle, which was selected with 'ButtonDownFcn'

3 次查看(过去 30 天)
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

采纳的回答

Walter Roberson
Walter Roberson 2024-6-27
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 个评论
Daniel Ludwig
Daniel Ludwig 2024-6-28
Hi Walter,
thanks for your reply, I unterstand.
Do you have any idea, how to "pull out the value"?
Walter Roberson
Walter Roberson 2024-6-28
编辑:Walter Roberson 2024-6-28
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by