Return of handle, which was selected with 'ButtonDownFcn'
41 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Daniel Ludwig
am 27 Jun. 2024 um 18:25
Bearbeitet: Walter Roberson
am 28 Jun. 2024 um 19:57
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);
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
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 27 Jun. 2024 um 19:43
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.)
2 Kommentare
Walter Roberson
am 28 Jun. 2024 um 19:56
Bearbeitet: Walter Roberson
am 28 Jun. 2024 um 19:57
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
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!