question about the function of callback

4 ビュー (過去 30 日間)
zhiping
zhiping 2011 年 5 月 17 日
hello, I have a question about the writing the function of callback. # how to callback a group of function? For exemple, We use 'callback' like this way: 'callback', @pushbutton_callback. Here pushbutton_callback is a function. In my interface there is many same functions repeated, Can I realize to write the function of callback like @pushbutton_callback(k), and there is the function of callback related. I know writing directly like pushbutton_callback(k) is not correct. I didn't find something similar. What should I do?
  2 件のコメント
Andy
Andy 2011 年 5 月 17 日
Can you clarify: do you want a single pushbutton to fire off multiple callback functions, or do you want to set the callbacks for all of your pushbuttons in one call?
zhiping
zhiping 2011 年 5 月 17 日
I used a loop to create multiple pushbuttons(pushbuttons(k)), and want to use a loop to write his own function ( pushbuttons_callback(k)). thanks in advance.

サインインしてコメントする。

採用された回答

Jan
Jan 2011 年 5 月 17 日
Function handles cannot be combined in an array due to the konfusion of the indexing and the input arguments. But you can create a cell of function handles:
C = {@sin, @cos, @tan};
for i = 1:3
feval(C{i}, pi)
% In modern Matlab versions also directly:
% C{i}(pi)
end
Similar works for pushbutton_callback{k}.
  1 件のコメント
zhiping
zhiping 2011 年 5 月 23 日
Thank you verry much

サインインしてコメントする。

その他の回答 (2 件)

Paulo Silva
Paulo Silva 2011 年 5 月 17 日
If you have several pushbuttons each one with his own function, try this:
hb = uicontrol('Style','pushbutton',...
'String','PushMe');
set(hb,'Callback',sprintf('pushbutton%d_callback',k))
%for button1
%pushbutton1_callback
This also might work (not tested)
hb = uicontrol('Style','pushbutton',...
'String','PushMe');
set(hb,'Callback',sprintf('@pushbutton%d_callback',k))
%for button1
%pushbutton1_callback

Yoav Livneh
Yoav Livneh 2011 年 5 月 17 日
Another option is to do this:
push_button(k) = uicontrol('style','pushbutton',...
'callback',{@pushbutton_callback_main,k});
and the callback function:
function pushbutton_callback_main(obj,event,idx)
% Run pushbutton_callback number idx
eval(['pushbutton_callback' idx]);
end
There's probably a way to do this without eval but can't think of one right now

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by