Return of handle, which was selected with 'ButtonDownFcn'
조회 수: 41 (최근 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);
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
채택된 답변
Walter Roberson
2024년 6월 27일 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
Walter Roberson
2024년 6월 28일 19:56
편집: Walter Roberson
2024년 6월 28일 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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!