如何在 MATLAB 中添加按钮组?
在 MATLAB 中,我们可以创建不同类型的图形用户界面 (GUI) 组件,如编辑字段、超链接、按钮等。在本教程中,我们将集中精力在 MATLAB 中添加按钮组。
MATLAB 中的按钮组是什么?
MATLAB 是一种环境,它提供了一种创建 GUI 应用程序的简便方法,而无需具备适当的计算机编程知识。在 MATLAB 中,有一个图形用户界面 (GUI) 组件,即按钮组,它允许我们创建一组单选按钮或选项按钮。一次只能选择组中的一个单选按钮。
MATLAB 中的这个 GUI 组件特别用于我们希望允许用户从一组选项中仅选择一个选项的应用程序中。
如何在 MATLAB 中创建按钮组?
在 MATLAB 中,有一个内置函数"uibuttongroup",用于在 MATLAB 应用程序中创建按钮组。此处描述了创建按钮组的分步过程。
步骤 (1) – 使用"uibuttongroup"函数创建按钮组。
步骤 (2) – 在组中创建单选按钮或选项按钮。为此,请使用"uicontrol"函数。
步骤 (3) – 为每个单选按钮设置一个回调函数。当用户选择组中的特定单选按钮时,此函数将执行。
因此,在 MATLAB 应用程序中创建按钮组是一个简单的三步过程。
现在,让我们借助示例实际了解如何在 MATLAB 中添加按钮组。
在 MATLAB 中使用默认属性创建按钮组
在 MATLAB 中,要创建具有默认属性的按钮组,请使用"uibuttongroup"函数的以下语法:
bg = uibuttongroup();
示例
以下 MATLAB 程序说明如何实现 MATLAB 代码以添加具有默认属性的按钮组。
% MATLAB 程序以默认属性添加 ButtonGroup % 创建 ButtonGroup 组件 bg = uibuttongroup(); % 在 ButtonGroup 中创建四个单选按钮 R1 = uicontrol(bg, 'Style', 'radiobutton', 'String', 'MATLAB', 'Position', [200 400 250 60]); R2 = uicontrol(bg, 'Style', 'radiobutton', 'String', 'Electrical', 'Position', [200 350 250 60]); R3 = uicontrol(bg, 'Style', 'radiobutton', 'String', 'Electronics', 'Position', [200 300 250 60]); R4 = uicontrol(bg, 'Style', 'radiobutton', 'String', 'Computer Science', 'Position', [200 250 250 60]);
输出
在指定的父容器内创建按钮组
在 MATLAB 中,要在指定的父容器内创建 GroupButton,我们使用"uibuttongroup"函数的以下语法:
bg = uibuttongroup(parent);
示例
以下 MATLAB 程序演示了在指定的父容器(图)内创建按钮组的 MATLAB 代码的实现。
% MATLAB 程序在父容器内添加按钮组 % 创建父容器 fig = uifigure('Name', 'Tutorials Point Courses', 'Position', [500, 500, 500, 300]); % 创建 ButtonGroup 组件 bg = uibuttongroup(fig, 'Position', [100, 50, 350, 200]); % 在 ButtonGroup 中创建四个单选按钮 R1 = uicontrol(bg, 'Style', 'radiobutton', 'String', 'Tutorials', 'Position', [10 70 150 30]); R2 = uicontrol(bg, 'Style', 'radiobutton', 'String', 'Video Lectures', 'Position', [10 40 150 30]); R3 = uicontrol(bg, 'Style', 'radiobutton', 'String', 'E-Books', 'Position', [170 70 150 30]); R4 = uicontrol(bg, 'Style', 'radiobutton', 'String', 'Articles', 'Position', [170 40 150 30]);
输出
创建具有自定义外观的按钮组
"uibuttongroup"的以下语法用于创建具有自定义外观和行为的按钮组:
bg = uibuttongroup(---, Name, Value,…);
此处,名称-值对用于指定按钮组的自定义属性。
示例
以下示例程序演示了此语法的实现。
% MATLAB 程序添加具有自定义属性的按钮组 % 创建父容器 fig = uifigure('Name', 'Tutorials Point Courses', 'Position', [500, 500, 500, 300]); % 创建 ButtonGroup 组件 bg = uibuttongroup(fig, 'Title', 'E-Books', 'Position', [100, 50, 350, 200]); % 在 ButtonGroup 中创建四个单选按钮 R1 = uicontrol(bg, 'Style', 'radiobutton', 'String', 'MATLAB', 'Position', [10 70 150 30]); R2 = uicontrol(bg, 'Style', 'radiobutton', 'String', 'Python', 'Position', [10 40 150 30]); R3 = uicontrol(bg, 'Style', 'radiobutton', 'String', 'Power Electronics', 'Position', [170 70 150 30]); R4 = uicontrol(bg, 'Style', 'radiobutton', 'String', 'Digital Electronics', 'Position', [170 40 150 30]);
输出
创建可滚动的按钮组
我们还可以在 MATLAB 中创建可滚动的按钮组。为此,我们只需将按钮组的"scrollable"属性设置为"on"。使用以下语法,
bg.Scrollable = 'on';
示例
考虑以下 MATLAB 程序以查看代码实现。
% MATLAB 程序添加可滚动的按钮组 % 创建父容器 fig = uifigure('Name', 'Tutorials Point Courses', 'Position', [500, 500, 500, 300]); % 创建 ButtonGroup 组件 bg = uibuttongroup(fig, 'Position', [100, 50, 350, 200]); % 在 ButtonGroup 中创建四个单选按钮 R1 = uicontrol(bg, 'Style', 'radiobutton', 'String', 'MATLAB', 'Position', [200 400 250 60]); R2 = uicontrol(bg, 'Style', 'radiobutton', 'String', 'Electrical', 'Position', [200 350 250 60]); R3 = uicontrol(bg, 'Style', 'radiobutton', 'String', 'Electronics', 'Position', [200 300 250 60]); R4 = uicontrol(bg, 'Style', 'radiobutton', 'String', 'Computer Science', 'Position', [200 250 250 60]); R5 = uicontrol(bg, 'Style', 'radiobutton', 'String', 'Power Electronics', 'Position', [200 200 250 60]); R6 = uicontrol(bg, 'Style', 'radiobutton', 'String', 'Digital Electronics', 'Position', [200 150 250 60]); % 启用可滚动按钮组 bg.Scrollable = 'on';
输出
结论
这就是在 MATLAB 中添加按钮组的全部内容。MATLAB 提供了一个内置函数"uibuttongroup",允许我们在 MATLAB 应用程序中创建一组单选按钮。在本教程中,我们解释了"什么是按钮组"以及如何在 MATLAB 中创建具有不同属性的不同类型的按钮组。