Callback function with pushbutton option
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Jan
2013-7-18
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 个评论
Jan
2013-7-18
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?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!