Returning a value from a function after click in figure

조회 수: 6 (최근 30일)
Markus Toivonen
Markus Toivonen 2018년 4월 18일
댓글: Walter Roberson 2018년 4월 18일
I would like to return a value from a function, based on the type of click that was used.
I have the following codes:
figure
i = 0;
plot(rand(5))
val = set(gca,'buttondownfcn',@mybttnfcn)
and
function value = mybttnfcn(h,~)
hf = get(h,'parent');
b = get(hf,'selectiontype');
if strcmpi(b,'normal')
value = 1;
disp(value)
elseif strcmpi(b,'alt')
value = -1;
disp(value)
else
value = 0;
disp(value)
end
I would want to capture the 'value' from the function, into a variable where the call happens. Thanks!

채택된 답변

Walter Roberson
Walter Roberson 2018년 4월 18일
Callbacks cannot return values (except for position constraints functions). You need to get the value out a different way.
https://www.mathworks.com/help/matlab/creating_guis/share-data-among-callbacks.html
  댓글 수: 2
Markus Toivonen
Markus Toivonen 2018년 4월 18일
Would it be possible to do this in one function?
h = figure;
i = 0;
plot(rand(5))
b = get(h,'selectiontype');
if strcmpi(b,'normal')
value = 1;
elseif strcmpi(b,'alt')
value = -1;
else
value = 0;
end
Currently, it does not react to clicks. I guess due to the lack of "ButtonDownFcn"?
Walter Roberson
Walter Roberson 2018년 4월 18일
https://www.mathworks.com/help/matlab/ref/waitforbuttonpress.html

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by