📜  MATLAB图形用户界面

📅  最后修改于: 2021-01-07 03:16:13             🧑  作者: Mango

MATLAB图形用户界面

图形用户界面(GUI)是程序的可视界面。优质的GUI通过为应用程序提供一致的外观和直观的控件(例如按钮,编辑框,列表框,滑块和菜单),可以使应用程序使用起来更加舒适。

图形用户界面如何工作?

图形用户界面为客户端提供了一个熟悉的工作环境。它包含按钮,切换按钮,列表,菜单,文本框等。对于程序员而言,GUI较难,因为基于GUI的应用程序必须随时为任何GUI项目的鼠标单击(或可能是键盘输入)做好准备。这样的数据称为事件,并且响应事件的程序被称为事件驱动的。

创建MATLAB图形用户界面所需的三个主要组件是:

组件

MATLAB GUI上的每个元素(按钮,标签,编辑框等)都是图形组件。组件的方法包括图形控件(按钮,切换按钮,编辑框,文件,滑块等),静态组件(文本框),菜单,工具栏和轴。 uicontrol函数创建图形控件和文本框,菜单由uimenu和uicontextmenu函数创建。工具栏按函数uitoolbar进行组织。由函数轴创建用于显示图形数据的轴。

货柜

GUI的组件必须组织在一个容器中,该容器是计算机屏幕上的一个窗口。最常见的容器是数字。数字是笔记本电脑屏幕上的窗口,顶部带有标题栏,并且可以有选择地连接菜单。过去,无论何时绘制数据,图片都是自动创建的。

函数图可以生成空图,并且空图可用于影响组件和其他容器的任何组合。容器的不同类型是面板(由uipanel函数创建)和按钮组(由uibuttongroup函数创建)。面板可以包含组件或其他容器,但是它们没有标题栏,也无法连接菜单。按钮组是特殊的面板,可以处理单选按钮或切换按钮的组,以确保该组中的任何一个按钮在任何时候都不会打开。

回呼

最后,如果用户在按钮上单击鼠标或在键盘上键入信息,则必须采取某种措施。鼠标单击或按键是一个事件,如果MATLAB程序要执行其函数,则必须对每个事件做出响应。例如,如果用户单击按钮,则该事件必须导致执行实现按钮角色的MATLAB代码。响应事件而执行的代码称为回调。必须有一个回调来实现GUI上每个图形元素的角色。

GUI的组件

Component Created By Description
Containers
Figure figure It creates a figure, which is a container that can hold components and other containers. Figures are independent windows that have title bars and can have menus.
Panel uipanel It creates a panel, which is a container that can hold components and other containers. Unlike images, panels do not have title bars or menus. Panels can be placed inside picture or other panels.
Button Group uibuttongroup It creates a button group, which is a special kind of panel. Button groups automatically handle groups of radio buttons or toggle buttons to ensure that only one component of the group is on at any given time.
Graphical Controls
Pushbutton uicontrol A graphical element that implements a pushbutton. It triggers the callback when clicked with a mouse.
Toggle Button uicontrol A graphical component that perform a toggle button. A toggle button is either “on” or “off,” and it change state each time it is clicked. Each mouse button click also triggers a callback.
Radio Button uicontrol A radio button is a type of toggle button that occur as a small circle with a dot in the middle when it is “on.” Groups of radio buttons are used to perform mutually exclusive choices. Each mouse click on a radio button triggers a callback.
Checkbox uicontrol A checkbox is a type of toggle button that occur as a small square with a check mark in it when it is “on.” Each mouse click on a checkbox triggers a callback.
Edit Box uicontrol An edit box displays a text string and allows the user to modify the information presented. A callback is triggered when the user presses the Enter key, or when the user clicks on a different object with the mouse.
List Box uicontrol A list box is a graphical control that shows a series of text strings. A customer may select one of the text strings by single- or double-clicking on them. A callback is triggered when the customer selects a string.
Popup Menus uicontrol A popup menu is a graphical control that shows a series of text strings in response to a mouse click. When the popup menu is not clicked on, only the currently selected string is clear.
Slider uicontrol A slider is a graphical control to adjust a value in a smooth, regular fashion by dragging the control with a mouse. Each slider modify triggers a callback.
Frame uicontrol It creates a frame, which is a rectangular box within a figure. Frames are used to association sets of controls together. Frames never trigger callbacks.
Text Field uicontrol It creates a label, which is a text string located at a point on the figure. Text fields never trigger callbacks.
Menus, Toolbars, Axes
Menu Items uimenu It creates a menu item. Menu items trigger a callback when a mouse button is released over them
Context Menus uicontextmenu It creates a context menu, which is a menu that appears over a graphical object when a user right-clicks the mouse on that object.
Toolbar uitoolbar It creates a toolbar, which is a bar across the top of the figure containing quick-access buttons.
Toolbar uipushtool It creates a pushbutton to go in a toolbar.
Toolbar Toggle uitoggletool It creates a toggle button to go in a toolbar.
Axes axes It creates a new set of axes to display data on. Axes never trigger callbacks.

将简单的计算器实现为GUI(图形用户界面)。

% --- Executes on button press in BtnZero.
function BtnZero_Callback (hObject, eventdata, handles)
% hObject    handle to BtnZero (see GCBO)
% eventdata reserved - to be described in a future version of MATLAB
% handles    structure with manage and user data (see GUIDATA)
textString=get (handles. edit1,'string');
textString=strcat(textString,'0');
set (handles. edit1,'string', textString);

% --- Executes on button press in BtnOne.
function BtnOne_Callback (hObject, eventdata, handles)
textString=get (handles. edit1,'string');
textString=strcat(textString,'1');
set (handles. edit1,'string', textString);
 
 % --- Executes on button press in BtnTwo.
function BtnTwo_Callback (hObject, eventdata, handles)
textString=get (handles. edit1,'string');
textString=strcat(textString,'2');
set (handles. edit1,'string', textString);
 
% --- Executes on button press in BtnThree.
function BtnThree_Callback (hObject, eventdata, handles)
textString=get (handles. edit1,'string');
textString=strcat(textString,'3');
set(handles. edit1,'string', textString);

% --- Executes on button press in BtnFour.
function BtnFour_Callback (hObject, eventdata, handles)
textString=get (handles. edit1,'string');
textString=strcat(textString,'4');
set (handles. edit1,'string',textString);
 
% --- Executes on button press in BtnFive.
function BtnFive_Callback(hObject, eventdata, handles)
textString=get (handles. edit1,'string');
textString=strcat(textString,'5');
set (handles. edit1,'string', textString);
 
% --- Executes on button press in BtnSix.
function BtnSix_Callback (hObject, eventdata, handles)
textString=get (handles. edit1,'string');
textString=strcat(textString,'6');
set (handles. edit1,'string', textString);

% --- Executes on button press in BtnSeven.
function BtnSeven_Callback (hObject, eventdata, handles)
textString=get (handles. edit1,'string');
textString=strcat(textString,'7');
set (handles. edit1,'string', textString);

 
% --- Executes on button press in BtnEight.
function BtnEight_Callback (hObject, eventdata, handles)
textString=get (handles. edit1,'string');
textString=strcat(textString,'8');
set (handles. edit1,'string', textString);
 

% --- Executes on button press in BtnNine.
function BtnNine_Callback (hObject, eventdata, handles)
textString=get (handles. edit1,'string');
textString=strcat(textString,'9');
set (handles. edit1,'string', textString);
 
% --- Executes on button press in BtnPlus.
function BtnPlus_Callback (hObject, eventdata, handles)
textString=get (handles. edit1,'string');
textString=strcat(textString,'+');
set(handles. edit1,'string', textString);
 
% --- Executes on button press in BtnMinus.
function BtnMinus_Callback (hObject, eventdata, handles)
textString=get (handles. edit1,'string');
textString=strcat(textString,'-');
set (handles. edit1,'string', textString);
 
% --- Executes on button press in BtnMul.
function BtnMul_Callback (hObject, eventdata, handles)
textString=strcat(textString,'*');
set (handles. edit1,'string', textString);
 
% --- Executes on button press in BtnDivide.
function BtnDivide_Callback (hObject, eventdata, handles)
textString=get (handles. edit1,'string');
textString=strcat(textString,'/');
set (handles. edit1,'string', textString);
 
% --- Executes on button press in BtnCalc.
function BtnCalc_Callback (hObject, eventdata, handles)
textString=get (handles. edit1,'string');
textString=eval(textString);
set (handles. edit1,'string', textString);
 
% --- Executes on button press in BtnClear.
function BtnClear_Callback (hObject, eventdata, handles)
textString=get (handles. edit1,'string');
set (handles. edit1,'string','');

输出: