How can I know and set Callbacks I created by functions??
2 次查看(过去 30 天)
显示 更早的评论
I previously asked how to create Callbacks for buttons that are automatically generated by functions... I found on the guide how to do it...
Now, I have to know if callbacks where created and how to set them... I post my code
function E0_creapb(institute, fig)
UNI=unique(institute);
for i=1:length(UNI)
b=['Institute' char(UNI(i))];
c=['PB_Inst_' char(UNI(i))];
end
N_PB = length(UNI); %numb of buttons
bh = zeros(N_PB,1);
%dimension of buttons
w=104;
h=22;
for k=1:N_PB
a=['Institute ' char(UNI(k))];
z=['PB_', b];
bh(k) = uicontrol(fig, 'Style', 'pushbutton', 'Tag', ...
z, 'Units', 'pixel', 'Position', ...
[(50+(w*1.5*(k-1))) 500 w h], 'String', a);
set(bh(k), 'Callback', {c})
end
This is the function that generate my push buttons and the last "set" generate callbacks... Can anyone help me???
0 个评论
采纳的回答
Walter Roberson
2012-1-3
Your loop initializing b and c keeps overwriting those variables.
When you set a callback to be a cell array, if the first entry is a function handle then that is the function to execute, and if the first entry is a string then the string will be evaluated as a function name. Are you sure you want to use PB_Inst_* as the name of the function to call?
4 个评论
更多回答(1 个)
Jethro
2012-1-4
1 个评论
Walter Roberson
2012-1-8
When you create a control in code, it is not automatically added to handles. You have to add it to handles yourself, such as
handles.(c) = b(k);
and then before the end of that routine,
guidata(bk(1), handles);
In my opinion, it is not good program design to use a different routine name for each button. How are you going to create the routines? In the form you are using, a string for the callback, the string must be the name of a function known to the base workspace (i.e., it must match the name of an existing .m file), or else (in theory) the string must be the name of a function handle stored in the base workspace. Are you planning on having your code write out individual .m files matching the names of the callbacks you are generating, or are you planning on creating those .m files before the run and hoping that you can match up the functionality of the pre-created .m files with the functionality you need for the individual pushbuttons ?
Things are much easier if you have a single callback routine and you pass to it a parameter the context it is being invoked in -- code such as I showed before.
Anyhow...
It is not clear what you mean by "recall" the callback. You can get() the 'Callback' property of the handle. This does have uses, but it is not the most common thing to do. I have been coming to find that if I am get()'ing the Callback of a routine for any reason other than to simulate that control having been invoked by the user, then I am probably creating something that is difficult to maintain and should be handled some other way.
另请参阅
类别
在 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!