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!