Callback function with pushbutton option
1 回表示 (過去 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!