Specify Callbacks in Function Calls matlab

本文详细介绍了MATLAB GUI中使用字符串回调、函数句柄回调和细胞数组回调的方法,并通过示例展示了如何在GUI中执行不同的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Specify a component callback property value as one of the following:

  • A string that contains one or more MATLAB ortoolbox commands to evaluate

  • A handle to a function that is within scope when theGUI is running

  • A cell array containing a string function name ora function handle, plus optional strings, constants, or variable namesfor arguments

Use String Callbacks

You can concatenate commands in a string callback. This one,for example, adds a title to the plot it creates.

set(hb,'Callback',...
       'plot(myvar,''--m''); title(''String Callback'')')
Use Function Handle Callbacks

Here is an example of declaring a callback when defininga uicontrol:

figure
uicontrol('Style','slider','Callback',@display_slider_value)

Hereis the definition of the function in the GUI code file. The callbackprints the value of the slider when you adjust it:

function display_slider_value(hObject,eventdata)
disp(['Slider moved to ' num2str(get(hObject,'Value'))]);

Whenyou click an arrow on the slider, the output of the function lookslike this:

Slider moved to 0.01
Slider moved to 0.02

Use Cell Arrays with Strings.

 The following cellarray callback defines a function name as a quoted string, 'pushbutton_callback',and two arguments, one a variable name and one a string:

myvar = rand(20,1);
set(hb,'Callback',{'pushbutton_callback',myvar,'--m'})

Thesetwo arguments are followed by whatever arguments you include whenyou specify the callback for the component. Code to execute 'pushbutton_callback' mightlook like this:

function pushbutton_callback(hObject, eventdata, var1, var2)
plot(var1,var2)

Use Cell Arrays with Function Handles.  

You canspecify a callback function using a function handle insteadof using a function name.

The following variation uses afunction handle to specify pushbutton_callback asthe callback routine to be executed when a user clicks Plotline.

figure;
hb = uicontrol('Style','pushbutton',...
               'String','Plot line')
set(hb,'Callback',{@pushbutton_callback,myvar,'--m'})

As above, the callback is in a file named pushbutton_callback.m,which contains code such as this:

function pushbutton_callback(hObject, eventdata, var1, var2)
plot(var1,var2)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值