在MATLAB中多个按钮的一个功能 [英] One function for multiple pushbuttons in matlab

查看:738
本文介绍了在MATLAB中多个按钮的一个功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想为多个按钮设置一个功能,他们都做同样的事情,但它有一个不同的定义值。这是为了当一个按钮被激活时,它不会与同一功能的其他按钮混淆。

I was thinking of setting one function for multiple pushbuttons, They all do the same thing, but it has a different defining value. This is so that when one pushbutton is activated it does not get mixed up with the other pushbutton of the same function

推荐答案

a href =http://www.mathworks.com/help/matlab/creating_plots/callback-definition.html =nofollow>回调的文档。回调默认接受两个输入参数:调用该函数的对象的句柄和来自对象的事件数据的结构,其可以或可以不是空的。您可以使用按钮的 String 属性来根据按下的按钮来控制GUI的行为单个回调函数。考虑下面的例子:

See the documentation for callbacks. Callbacks accept two input arguments by default: the handle of the object that invoked the function and a structure of event data from the object, which may or may not be empty. You can use the String or Tag properties of your pushbutton to control behavior of your GUI based on what button is pressed using a single callback function. Consider the following example:

function testGUI
handles.mainwindow = figure();

handles.mytextbox = uicontrol( ...
    'Style', 'edit', ...
    'Units', 'normalized', ...
    'Position', [0.15 0.80 .70 .10], ...
    'String', 'No Button Has Been Pressed' ...
    );
handles.button(1) = uicontrol( ...
    'Style', 'pushbutton', ...
    'Units', 'normalized', ...
    'Position', [0.05 0.05 .30 .70], ...
    'String', 'Button1', ...
    'Callback', {@mybuttonpress,handles} ...
    );
handles.button(2) = uicontrol( ...
    'Style', 'pushbutton', ...
    'Units', 'normalized', ...
    'Position', [0.35 0.05 .30 .70], ...
    'String', 'Button2', ...
    'Callback', {@mybuttonpress,handles} ...
    );
handles.button(3) = uicontrol( ...
    'Style', 'pushbutton', ...
    'Units', 'normalized', ...
    'Position', [0.65 0.05 .30 .70], ...
    'String', 'Button3', ...
    'Callback', {@mybuttonpress,handles} ...
    );
end

function mybuttonpress(src, ~, handles)
switch src.String
    case 'Button1'
        handles.mytextbox.String = 'Button 1 Has Been Pressed';
    case 'Button2'
        handles.mytextbox.String = 'Button 2 Has Been Pressed';
    case 'Button3'
        handles.mytextbox.String = 'Button 3 Has Been Pressed';
    otherwise
        % Something strange happened
end
end

请注意,这需要MATLAB R2014b或更新版本,以便使用点符号访问对象属性。请参见此博文了解更多信息。

Note that this requires MATLAB R2014b or newer in order to use the dot notation for accessing object properties. See this blog post for more information.

这篇关于在MATLAB中多个按钮的一个功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆