pass string variable to a function in uicontrol callback

8 次查看(过去 30 天)
Hello,
I have a function which draws push-buttons and calls back another function. But it must pass the following three variables to the callback function:
function drawPushButtons(num1,randomVector,string1)
hCorrectAns=uicontrol(gcf,...
'Style','push',...
'Units','normalized', 'Position',[0.6 0 0.3 0.15],...
'FontName','Arial Unicode MS', 'FontSize',12,...
'String','Correct Answer',...
'CallBack',@correctAnswer);
end
Then, the callback function executes a code :
function correctAnswer(hCorrectAns, EventData)
for iAns=1:num1
%use randomVector to manipulate the variable 'string1'.
%There is uicontrol in this function also.
end
end
Unfortunately, even after trying various MATLAB Answers related to this type of query, the problem remains. How to pass these three variables from the uicontrol pushbutton to external callback function?
P.S. - declaring global is not an option. Thanks.

采纳的回答

Stephen23
Stephen23 2020-10-22
编辑:Stephen23 2020-10-22
Define the function to accept five input arguments:
function correctAnswer(hCorrectAns,EventData,num1,randomVector,string1)
and provide the extra arguments by specifying the callback inside a cell vector:
'CallBack',{@correctAnswer,num1,randomVector,string1}
This approach is documented here:
(Of course the names used inside the function are irrelevant to what they are named outside the callback)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by