pass string variable to a function in uicontrol callback

13 vues (au cours des 30 derniers jours)
v k
v k le 22 Oct 2020
Commenté : v k le 25 Oct 2020
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.

Réponse acceptée

Stephen23
Stephen23 le 22 Oct 2020
Modifié(e) : Stephen23 le 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)

Plus de réponses (0)

Catégories

En savoir plus sur Migrate GUIDE Apps dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by