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

时间:2025-05-14

NATLAB图形设计

实验六 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(水平左对齐)。

NATLAB图形设计

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

NATLAB图形设计

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);

NATLAB图形设计

② 设计一个按学期和科目查询的学生成绩查询系统,如下图所示。可查询的学期有:“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

…… 此处隐藏:1510字,全部文档内容请下载后查看。喜欢就下载吧 ……
实验六 MATLAB图形用户界面设计.doc 将本文的Word文档下载到电脑

    精彩图片

    热门精选

    大家正在看

    × 游客快捷下载通道(下载后可以自由复制和排版)

    限时特价:7 元/份 原价:20元

    支付方式:

    开通VIP包月会员 特价:29元/月

    注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
    微信:fanwen365 QQ:370150219