Function does not return value when nested
조회 수: 9 (최근 30일)
이전 댓글 표시
Hello community,
I have encountered a slight issue with a function not returning a value when nested. My main function calls a second function, which plots some data. Within this data a button is created, which should activate a (obviously nested) callback function, which closes the figure and returns a variable, to which the second function should react. The problem is, that the nested callback function does not seem to return this value, even though it definitely is called (it closes the figure). The entire routine works, when the second function (the one creating the button, and initializing the callback) is NOT nested inside another function.
Does anyone know the reason for this, and how to solve the issue?
here is an excerpt of my nested second function:
figure(1)
d = gcf;
btn1 = uicontrol('Parent',d,...
'Position',[400 100 330 100],...
'FontSize',12,...
'String','Draw again',...
'Callback','[button_choice] = press_button_1');
uiwait
% here is where the second function breaks, when nested, as button_choice is not defined
if button_choice == 1
% do something
end
and this is the callback function
function [button_choice] = press_button_1
button_choice = 1;
delete(gcf)
end
댓글 수: 1
답변 (2개)
Adam
2017년 1월 19일
Callback functions cannot return arguments.
Since it is a nested function though it shares the workspace of the parent function so you can just define
button_choice = [];
(or some other default if you wish) in your main function and then just set this variable in the nested function rather than returning it.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!