Callback function with pushbutton option
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (1)
Jan
am 18 Jul. 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 Kommentare
Jan
am 18 Jul. 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?
Siehe auch
Kategorien
Mehr zu Migrate GUIDE Apps finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!