pass string variable to a function in uicontrol callback

11 views (last 30 days)
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.

Accepted Answer

Stephen23
Stephen23 on 22 Oct 2020
Edited: Stephen23 on 22 Oct 2020
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)

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!