matlab gui各控件使用模板记录及说明

最近重新学习了一下matlab GUI 的程序编写,用的是guide。虽然matlab现在出了app来编写GUI界面,但是以前的guide还是有使用,官方给出的文档给出了各种控件使用的模板例程,给出的例程都非常简单易懂,需要实现其他的功能只要对下面的程序稍加修改一下各个状态的代码即可,这里记录并加以注释,方便理解各个控件最基本的原理,方便后续查阅使用:

1.Push Button 普通按钮

1.1 图标

普通按钮

1.2 说明

释放鼠标按键前显示为按下状态的按钮。

1.3 例程

function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

display('Goodbye'); %命令行窗口显示“Goodbye”
close(gcf); %关闭窗口

原文解释

The first line of code, display(‘Goodbye’), displays ‘Goodbye’ in the Command Window. The next line gets the UI window using gcf and then closes it.

实现功能

按下Push Button命令行窗口显示“Goodbye”,然后关闭整个界面的窗口。

2.Toggle Button 切换按钮

2.1 图标

切换按钮

2.2 说明

按钮,它在外观上类似于普通按钮,但在视觉上有状态指示:选中或清除。

2.3 例程

function togglebutton1_Callback(hObject,eventdata,handles)
% hObject handle to togglebutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of togglebutton1

button_state = get(hObject,'Value'); %获取Toggle Button的Value为state
if button_state == get(hObject,'Max') %Toggle Button的state为Max
	display('down'); %命令行窗口显示“down”
elseif button_state == get(hObject,'Min') %Toggle Button的state为Min
 	display('up'); %命令行窗口显示“up”
end

原文解释

The toggle button’s Value property matches the Min property when the toggle button is up. The Value changes to the Max value when the toggle button is depressed. This callback function gets the toggle button’s Value property and then compares it with the Max and Min properties. If the button is depressed, then the function displays ‘down’ in the Command Window. If the button is up, then the function displays ‘up’.

实现功能

按下Toggle Button命令行窗口显示“down”,不按下Toggle Button命令行窗口显示“up”。

前提条件

设定Toggle Button的Value应该是Min的值(对应“up”,即不按下Toggle Button),否则逻辑就反过来了。

3.Radio Button 单选按钮

3.1 图标

单选按钮

3.2 说明

  • 作为组的一部分的选项,选中它时会清除组中的其他选项。
  • 要为一组单选按钮实现互斥行为,请将它们置于 uibuttongroup 中。

3.3 例程

function radiobutton1_Callback(hObject, eventdata, handles)
% hObject handle to radiobutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of radiobutton1

if (get(hObject,'Value') == get(hObject,'Max')) %Radio Button的Value是Max
	display('Selected');%命令行窗口显示“Selected”
else %Radio Button的Value是Min
	display('Not selected');%命令行窗口显示“Not selected”
end

原文解释

The radio button’s Value property matches the Min property when the radio button is not selected. The Value changes to the Max value when the radio button is selected. This callback function gets the radio button’s Value property and then compares it with the Max and Min properties. If the button is selected, then the function displays ‘Selected’ in the Command Window. If the button is not selected, then the function displays ‘Not selected’.

实现功能

选中Radio Button命令行窗口显示“Selected”,不选中Radio Button命令行窗口显示“Not selected”。

前提条件

设定Radio Button的Value应该是Min的值(对应“Not selected”,即不选中Radio Button),否则逻辑就反过来了。

4.Check Box 复选框

4.1 图标

复选框

4.2 说明

可以单独选择或清除的选项。

4.3 例程

function checkbox1_Callback(hObject, eventdata, handles)
% hObject handle to checkbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of checkbox1

if (get(hObject,'Value') == get(hObject,'Max'))%Check Box的value是Max
 display('Selected');%命令行窗口显示“Selected”
else %Check Box的value是Min
 display('Not selected');%命令行窗口显示“Not selected”
end

原文解释

The check box’s Value property matches the Min property when the check box is not selected. The Value changes to the Max value when the check box is selected. This callback function gets the check box’s Value property and then compares it with the Max and Min properties. If the check box is selected, the function displays ‘Selected’ in the Command Window. If the check box is not selected, it displays ‘Not selected’.

实现功能

选中Check Box命令行窗口显示“Selected”,不选中Check Box命令行窗口显示“Not selected”。

前提条件

设定Check Box的Value应该是Min的值(对应“Not selected”,即不选中Check Box),否则逻辑就反过来了。

5.Edit Text Field 可编辑文本

5.1 图标

可编辑文本

5.2 说明

  • 可编辑的文本字段。
  • 要启用多行文本,请设置 Max 和 Min 属性以满足 Max-Min > 1。

5.3 例程1:文本输入和获取

function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit1 as text
% str2double(get(hObject,'String')) returns contents as double

input = get(hObject,'String');%获取Edit Text Field中的字符串
display(input);%回车或者鼠标点击其他非文本框的地方命令行窗口显示输入的字符串

原文解释

When the user types characters inside the text field and presses the Enter key, the callback function retrieves those characters and displays them in the Command Window.

实现功能

在可编辑文本框中输入字符串,然后按回车或者鼠标点击其他非文本框的地方命令行窗口显示输入的字符串。

5.4 例程2 :数字输入和获取

function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit1 as text
% str2double(get(hObject,'String')) returns contents as a double

input = str2double(get(hObject,'String')); %获取文本框中的String并转换为double类型
if isnan(input) %如果输入的String不是数字(nonnumeric)
 errordlg('You must enter a numeric value','Invalid Input','modal') %错误警告对话框
 uicontrol(hObject) %关闭错误警告对话框仍使光标在可编辑文本框中变为活动状态并闪烁。
 return
else %如果输入的String是数字(numeric)
 display(input); %回车或者鼠标点击其他非文本框的地方命令行窗口显示输入的数字
end

原文解释

When the end user enters values into the edit text field and presses the Enter key, the callback function gets the value of the String property and converts it to a numeric value. Then, it checks to see if the value is NaN (nonnumeric). If the input is NaN, then the callback presents an error dialog box.

实现功能

在可编辑文本框中输入数字字符串,str2double函数将字符串转化为数字,输入完毕回车或者鼠标点击其他非文本框的地方,如果输入的不是数字,会弹出错误警告对话框,提示输入的不是数字!否则命令行窗口显示输入的数字。
在这里插入图片描述

6.Slider 滚动条

6.1 图标

在这里插入图片描述

6.2 说明

用户沿水平或垂直滑动条移动的“滑块”按钮。按钮沿滑块的位置表示指定范围内的值。

6.3 例程

function slider1_Callback(hObject, eventdata, handles)
% hObject handle to slider1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine...

slider_value = get(hObject,'Value'); %获取Slider的Value
display(slider_value); %命令行窗口显示slider的Value值

原文解释

When the end user moves the slider, the callback function gets the current value of the
slider and displays it in the Command Window. By default, the slider’s range is [0, 1]. To
modify the range, set the slider’s Max and Min properties to the maximum and minimum
values, respectively.

实现功能

移动Slider的滑块,显示滑块当前的值(介于[Min, Max])在命令行窗口,默认的Min是0,Max是1,这可以调节。

7.List Box 列表框

7.1 图标

在这里插入图片描述

7.2 说明

  • 用户可从中选择一项或多项的项列表。与弹出式菜单不同,点击列表框时不会展开。
  • 要启用多项选择,请设置 Max 和 Min 属性以满足 Max-Min > 1。要在可以从一个列表框中选择多项时延迟操作,您可以将完成普通按钮与该列表框相关联。然后,使用该按钮的回调来计算列表框 Value 属性。

7.3 例程

function listbox1_Callback(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns contents
% contents{get(hObject,'Value')} returns selected item from listbox1

items = get(hObject,'String'); %List Box中获取所有的字符串
index_selected = get(hObject,'Value'); %获取List Box中选中的字符串的索引
item_selected = items{index_selected}; %得到List Box中选中的字符串
display(item_selected); %命令行窗口显示List Box中选中的字符串

原文解释

When the end user selects an item in the list box, the callback function performs the
following tasks:
• Gets all the items in the list box and stores them in the variable, items.
• Gets the numeric index of the selected item and stores it in the variable,
index_selected.
• Gets the value of the selected item and stores it in the variable, item_selected.
• Displays the selected item in the MATLAB Command Window.

实现功能

在List Box中选中字符串,同时在命令行窗口打印选中字符串。

8.Pop-Up Menu 弹出式菜单

8.1 图标

在这里插入图片描述

8.2 说明

弹出式菜单(也称为下拉菜单),展开以显示选项列表。关闭时,弹出式菜单指示出当前选项。如果要提供许多互斥选项,请使用弹出式菜单。

8.3 例程

function popupmenu1_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns contents...
% contents{get(hObject,'Value')} returns selected item...

items = get(hObject,'String');%Pop-Up Menu中获取所有的字符串
index_selected = get(hObject,'Value');%获取Pop-Up Menu中选中的字符串的索引
item_selected = items{index_selected}; %得到Pop-Up Menu中选中的字符串
display(item_selected); %命令行窗口显示Pop-Up Menu中选中的字符串

原文解释

When the user selects an item in the pop-up menu, the callback function performs the
following tasks:
• Gets all the items in the pop-up menu and stores them in the variable, items.
• Gets the numeric index of the selected item and stores it in the variable,
index_selected.
• Gets the value of the selected item and stores it in the variable, item_selected.
• Displays the selected item in the MATLAB Command Window.

实现功能

在Pop-Up Menu中选中字符串,同时在命令行窗口打印选中字符串。

参考资料

[1]《App Building》.Mathworks R2017a : 7-11~7-22

  • 0
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然!我可以为您提供一些关于MATLAB GUI的基本教程。MATLAB GUI(图形用户界面)允许您创建交互式应用程序,使用户能够使用图形界面而不是命令行来与MATLAB进行交互。 下面是一个简单的步骤,帮助您入门MATLAB GUI的开发: 1. 打开MATLAB并创建一个新的GUI。您可以通过在命令行输入 "guide" 并按下回车键来打开MATLAB GUI编辑器。 2. 在GUI编辑器中,您可以使用工具箱中的各种控件(如按钮、文本框、复选框等)来设计GUI界面。您可以从左侧的“工具箱”窗格中选择控件,并将其拖放到右侧的“图形布局编辑器”窗格中。 3. 在拖放控件后,您可以通过在“属性编辑器”窗格中更改其属性和外观来自定义它们。例如,您可以更改按钮的标题、文本框的默认值等。 4. 您还可以为每个控件添加回调函数,以响应用户与GUI的交互。通过单击相应控件上的“回调函数”按钮,GUI编辑器将自动生成一个回调函数的模板。您可以在这个模板中编写自己的代码来实现所需的功能。 5. 当您完成GUI设计和回调函数编写后,可以通过点击“运行”按钮来运行GUI。这将打开一个新窗口,显示您设计的GUI界面。 这只是一个简单的入门指南,帮助您开始学习MATLAB GUI的开发。随着实践和经验的积累,您可以进一步探索更高级的功能和技术,以满足特定的应用需求。希望这些简单的指导对您有所帮助!如果您有任何进一步的问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值