学习资料
常用网站:https://ww2.mathworks.cn/solutions.html?s_tid=gn_sol
https://github.com/
https://www.ilovematlab.cn/
电子书:matlab gui设计学习笔记 罗华飞著
公众号:打浦桥程序员
视频资源:bilibili up主 <我不是matlab> 打浦桥程序员
一、
function text1
global GUI
%创建一个窗口
GUI.h=figure('units','pixels',...//单位定义为像素
'position',[10 50 450 250],...//位置坐标10,50,长450,宽250
'name','A',...//名字定义为A
'menubar','none',...//没有菜单条
'toolbar','none',...//关闭工具条
'numbertitle','off',...//关闭数字标签,不关闭的话默认名字是figure1:A
'resize','off');//将鼠标拖动重定义figure大小关闭掉
movegui(GUI.h,'center');//将窗口移动到中间,还有north、west、northwest...
%创建一个按钮
GUI.button=uicontrol('Parent',GUI.h,'Style','pushbutton','String','button',...
'position',[140 197 50 30],'visible','on',...
'callback',@ChangeEditFcn);//定义回调函数,@创建函数句柄
%创建一个文本
GUI.text1=uicontrol('Parent',GUI.h,...
'Style','Text',...
'string','出席人数设置',...
'foregroundColor','red',...
'Position',[10 160 125 22],...
'Fontsize',12,...
'Fontweight','bold',...
'HorizontalAlignment','left');
%总出席人数
GUI.text2=uicontrol('Parent',GUI.h,...
'Style','Text',...
'string','总出席人数',...
'foregroundColor','black',...
'Position',[10 120 125 22],...
'Fontsize',12,...
'Fontweight','bold',...
'HorizontalAlignment','left');
%显示总共多少人
GUI.text3=uicontrol('Parent',GUI.h,...
'Style','Text',...
'string','10',...
'foregroundColor','black',...
'Position',[153 120 125 22],...
'Fontsize',12,...
'Fontweight','bold',...
'HorizontalAlignment','left');
%可输入编辑人数框,默认为10
GUI.edit=uicontrol('Parent',GUI.h,...
'Style','Edit',...
'string','10',...
'BackgroundColor','g',...
'Position',[140 157 50 30],...
'Fontsize',12,...
'Fontweight','bold',...
'visible','on');
end
%函数效果为点击button,人数加1,总人数颜色红绿切换
function ChangeEditFcn(~,~) //~代表空输入输出
global GUI
EditValue=get(GUI.edit,'string');
EditValue=str2double(EditValue)+1;
EditValue=num2str(EditValue);%将数字转换为字符串
set(GUI.edit,'string',EditValue);
set(GUI.text3,'string',EditValue);
EditValue=str2double(EditValue);
if mod(EditValue,2)==0
set(GUI.text3,'foregroundColor','green')
else
set(GUI.text3,'foregroundColor','red')
end
end
如下所示:
注释快捷键:ctrl+R,取消 ctrl+T
:持续补充中>>>