第一范文网 - 专业文章范例文档资料分享平台

实验六 MATLAB图形用户界面设计

来源:用户分享 时间:2025/11/15 14:11:53 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

实验六 MATLAB图形用户界面设计(3学时)

1 实验的目的及意义:

(1)熟悉MATLAB图形用户界面开发环境。

(2)掌握MATLAB中图形用户界面程序设计方法。

2 实验步骤:

上机调试

① 设计如下图所示的简单四则运算计算器。

1) 在GUI设计窗口中放置16个Pushbutton控件,将其Tag和String属性分别设置为:

Pushbutton1,1 Pushbutton2,2 ……

Pushbutton9,9 Pushbutton10,0

Pushbutton_CLR,CLR Pushbutton_EQU,= Pushbutton_ADD,+ Pushbutton_SUB,- Pushbutton_MUL,× Pushbutton_DIV,÷

2) 在GUI设计窗口中放置2个Statictext控件,将其Tag和String属性分别设置为:

Text1,计算器 Text_ANS,0

再将Text_ANS控件的Backgroundcolor属性设置为白色;HorizontalAlignment属性设置为Left(水平左对齐)。

3) 设计Callback函数:(提示)

某一数字按键按下时,要将该按键的String属性读出来并连接到Text_ANS的String属性之后。(使用字符串连接函数Strcat)

function pushbutton1_Callback(hObject, eventdata, handles) str=get(handles.pushbutton1,'string');

str=strcat(get(handles.text_ANS,'string'),str); set(handles.text_ANS,'string',str);

function pushbutton2_Callback(hObject, eventdata, handles) str=get(handles.pushbutton2,'string');

str=strcat(get(handles.text_ANS,'string'),str); set(handles.text_ANS,'string',str); ....

function pushbutton9_Callback(hObject, eventdata, handles) str=get(handles.pushbutton9,'string');

str=strcat(get(handles.text_ANS,'string'),str); set(handles.text_ANS,'string',str);

function pushbutton10_Callback(hObject, eventdata, handles) str=get(handles.pushbutton10,'string');

str=strcat(get(handles.text_ANS,'string'),str); set(handles.text_ANS,'string',str);

CLR按键按下时,将Text_ANS的String属性设为空。

function pushbutton_CLR_Callback(hObject, eventdata, handles)

set(handles.text_ANS,'string','');

加、减、乘、除按键按下时,将Text_ANS的String属性读出来,并将字符串转换为整数存储到变量num1中;再将Text_ANS的String属性设为空;并给运算标志变量calculat_type赋值,加、减、乘、除分别对应calculat_type=1、2、3、4。由于num1和calculat_type这2个变量在pushbutton_EQU_Callback函数中也要用到,因此要设为全局变量(详见教材7.3.2节)。

function pushbutton_ADD_Callback(hObject, eventdata, handles)

global num1 calculat_type

str=get(handles.text_ANS,'string'); num1=str2num(str);

set(handles.text_ANS,'string',''); calculat_type=1;

function pushbutton_SUB_Callback(hObject, eventdata, handles)

global num1 calculat_type

str=get(handles.text_ANS,'string'); num1=str2num(str);

set(handles.text_ANS,'string',''); calculat_type=2;

function pushbutton_MUL_Callback(hObject, eventdata, handles)

global num1 calculat_type

str=get(handles.text_ANS,'string'); num1=str2num(str);

set(handles.text_ANS,'string',''); calculat_type=3;

function pushbutton_DIV_Callback(hObject, eventdata, handles)

global num1 calculat_type

str=get(handles.text_ANS,'string'); num1=str2num(str);

set(handles.text_ANS,'string',''); calculat_type=4;

等号键按下时,将Text_ANS的String属性读出来,并将字符串转换为整数存储到变量num2中;再根据calculat_type的值将num1和num2进行运算,将运算结果转换为字符串后设置为Text_ANS的String属性。

function pushbutton_EQU_Callback(hObject, eventdata, handles)

global num1 calculat_type

str=get(handles.text_ANS,'string'); num2=str2num(str); switch calculat_type case 1

result=num1+num2; case 2

result=num1-num2; case 3

result=num1*num2; case 4

result=num1/num2; end

str=num2str(result);

set(handles.text_ANS,'string',str);

② 设计一个按学期和科目查询的学生成绩查询系统,如下图所示。可查询的学期有:“2005~2006学年第一学期”、“2005~2006学年第二学期”、“2006~2007学年第一学期”、“2006~2007学年第二学期”。

参照教材【例6-3-3】,将Pop-upmenu控件的String属性设为4行字符,分别为:“2005~2006学年第一学期”、“2005~2006学年第二学期”、“2006~2007学年第一学期”、“2006~2007学年第二学期”。

function Seek_button_Callback(hObject, eventdata, handles) score=[19 19 97 8 0 47 29 94 3 41 64 84 23 54 34 58 37 93 40 75 7 86 79 66

50 39 83 52 15 56 8 55]; subject=get(handles.listbox1,'Value'); term=get(handles.popupmenu1,'Value'); str='';

for k=1:length(subject)

str2=num2str(score(term,subject(k))); str=char(str,str2);

set(handles.score_text,'String',str) end

搜索更多关于: 实验六 MATLAB图形用户界面设计 的文档
实验六 MATLAB图形用户界面设计.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c1icr79cxzd3fre38i3um_1.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top