Callback function

5 ビュー (過去 30 日間)
Sven Schoeberichts
Sven Schoeberichts 2011 年 12 月 14 日
Hi all,
I am trying to implement a piece of code (a function) into my script. The original code comes from a function. Running my script gives me the following error:
Error: Function definitions are not permitted in this context.
So I put a function declaration in a script, which is causing the error.
I tried putting the function in a seperate function file, but the function needs some variables that are used in the script. So provide the functions with some in- and output variables. But that's the part I am stuck. The functions is called by a callback, and I can't seem to add variables to it. Here is the piece of code which calls the function:
htimes1 = uicontrol( 'Style', 'pushbutton', 'String', '* ', 'Callback', {@bpmaall_Callback});
The function associated with this callback is:
function bpmaall_Callback(source, eventdata)
assenbp=ylim;
assenfft=xlim;
deltaxas1=0.05*assenbporg;
ynewbp= assenbp(2)-deltaxas1;
plot(frequency(1:80),fftsysbp(1:80),'r')
axis([0 assenfft(2) 0 ynewbp])
end
Does someone know a way to avoid this problem?
The piece of code is used to change the y-axis when a button is pressed. Any help is welcome.
- Sven

採用された回答

Malcolm Lidierth
Malcolm Lidierth 2011 年 12 月 14 日
You need
... 'Callback', {@bpmaall_Callback,x,y,uv});%no ()

その他の回答 (2 件)

Sean de Wolski
Sean de Wolski 2011 年 12 月 14 日
Pass in the additional input arguments to the function:
function bpmaall_Callback(source, eventdata,x,y,uv)
  1 件のコメント
Sven Schoeberichts
Sven Schoeberichts 2011 年 12 月 14 日
I tried that, but the problem is that Matlab won't accept any arguments in the callback, like:
... 'Callback', {@bpmaall_Callback(x,y,uv)});
The brackets will be red-underlined (Parse error at '}')

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


Fangjun Jiang
Fangjun Jiang 2011 年 12 月 14 日
I believe you should use either of this:
htimes1 = uicontrol( ..., 'Callback', @bpmaall_Callback)
htimes1 = uicontrol( ..., 'Callback', 'bpmaall_Callback')

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by