How to call the Callback function (in a subfunction of another Callback function)?

23 ビュー (過去 30 日間)
The following is my code:
function pushbutton1_Callback(hObject, eventdata, handles)
body
function pushbutton2_Callback(hObject, eventdata, handles)
subfun(hObject, eventdata, handles) % sub(...) is a subfunction or a .m file
function subfun(hObject, eventdata, handles)
pushbutton1_Callback(hObject, eventdata, handles) % call pushbutton1_Callback
It's strange that matlab show the error message:
Undefined function 'pushbutton1_Callback' for input arguments of type 'struct'. I want to know how to call the function 'pushbutton1_Callback()' in function 'subfun()'.Thanks

採用された回答

Sean de Wolski
Sean de Wolski 2013 年 3 月 4 日
You will have to pass the subfunction handles back from the main function:
%%%begin main.m%%%%
function [y, fh] = main(x)
y = pi*x;
fh = @subfun
function Q = subfun(a,b)
Q = a+b
%%%end main.m%%%
Now when you call main: you get the function handle back.
[y,fh] = main(2);
fh(1,2);
  1 件のコメント
Darshan Shah
Darshan Shah 2016 年 11 月 25 日
Can you explain this in detail. I am struggling to use this explanation. I am new to GUIDE and programming.

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

その他の回答 (2 件)

Jan
Jan 2013 年 3 月 4 日
The error message means, that:
  • either the function pushbutton1_Callback is not visible ftom subfun(), e.g. if the callback is a subfunction of an M-file, but subfun() is stored in another M-file.
  • or you use a struct as first input, while the callback requires the handle of the button as 1st input:
pushbutton1_Callback(ButtonHandle, EventData, handles)
Here EventData could be the empty matrix, if the callback function can handle this.

QiQin Zhan
QiQin Zhan 2013 年 3 月 4 日
I think it may be the first error you've said.
If I have a experment.m file,the code is as follows
function test1
x = 1
function test2
y = 0
Then how can I call the function 'test1' and 'test2' in another .m file?
  2 件のコメント
Jan
Jan 2013 年 3 月 4 日
編集済み: Jan 2013 年 3 月 4 日
@Chan: Is this a comment? If so, does it concern my answer? If so, please post this as a comment to reduce the confusion level.
Only the first function of an M-file can be called from other M-files. So either include a wrapper, such that the 1st function forwards the call to different subfunctions, or save the functions to individual M-files.
QiQin Zhan
QiQin Zhan 2013 年 3 月 7 日
I think I've got exact answer from Sean.It's an excellent idea to use function handle to call the second function from other M-files.Anyhow I would thank you very much.

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

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by