Callback function with pushbutton option
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi all,
I'm trying to configure an uicontrol gui of pushbutton type. I would like to create several pushbuttons and depending which one is pressed, the code will execute a specific function. I have problems to build the function. I'm new on this staff.
My code is:
ad=uicontrol('Style', 'pushbutton', 'String', 'Addition Beam','Position', [20 20 80 30],'Callback', @addition);
function addition
for j= 1:p
if J(j) == 2 || J(j) == 3,
J(j) = 0;
end
end
end
When the user clicks on the generated button 'Addition Beam', the function addition should be activated.
Thanks for your help.
Best regards,
Raúl.
0 commentaires
Réponses (1)
Jan
le 18 Juil 2013
This happens already: When the button is called, the function addition is called. But Matlab provides two standard inputs as default, so you need:
function addition(ObjectH, EventData)
for j = 1:p
if J(j) == 2 || J(j) == 3,
J(j) = 0;
end
end
end
But now the next occurring problem is, that neither p nor J are known inside this function. Any function has its own workspace and if you want to share a variable, you have to do this explicitly (together with a "vectorized" solution):
function addition(ObjectH, EventData)
handles = guidata(ObjectH); % Get struct from figure
J = handles.J;
J(J == 2 | J == 3) = 0;
handles.J = J;
guidata(ObjectH, handles); % Put struct to figure
end
2 commentaires
Jan
le 18 Juil 2013
Please post an exact copy of the complete error message. If Matlab moans about the function keyword, usually somebody tries to define a function inside a script or in the command window. But functions are allowed as main or sub-function in an M-file only. Therefore I thought your code is only a fragment of a larger M-file, but perhaps it is not. Are you working with GUIDE? Do you know the difference between scripts and functions?
Voir également
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!