How can I know and set Callbacks I created by functions??
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuesta aceptada
Walter Roberson
el 3 de En. de 2012
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 comentarios
Walter Roberson
el 3 de En. de 2012
http://www.mathworks.com/help/techdoc/creating_plots/f7-55506.html
Más respuestas (1)
Jethro
el 4 de En. de 2012
1 comentario
Walter Roberson
el 8 de En. de 2012
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.
Ver también
Categorías
Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!