필터 지우기
필터 지우기

Return of handle, which was selected with 'ButtonDownFcn'

조회 수: 41 (최근 30일)
Daniel Ludwig
Daniel Ludwig 2024년 6월 27일 18:25
편집: Walter Roberson 2024년 6월 28일 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);
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일 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.)
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일 5:49
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일 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 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