参考链接:https://www.bilibili.com/video/BV16f4y147x9/?spm_id_from=333.999.0.0
一、控制信号灯
% Callbacks that handle component events
methods (Access = private)
% Value changed function: Switch
function SwitchValueChanged(app, event)
value = app.Switch.Value;
%if条件判断
if value == "On"
app.Lamp.Color = "1.00,0.00,0.00"
else
app.Lamp.Color = "0.00,1.00,0.00"
end
end
end
二、按条件控制信号灯
% Value changed function: temp_value
function temp_valueValueChanged(app, event)
%定义环境温度
value = app.temp_value.Value;
if value>= 500
app.Lamp.Color="1.00,0.00,0.00";
app.show_tips.Value = "温度过高";
elseif value < 500 && value>=400
app.Lamp.Color="1.00,0.41,0.16";
app.show_tips.Value = "温度适中";
else
app.Lamp.Color="0.30,0.75,0.93";
app.show_tips.Value = "温度过低";
end
end
end
三、显示图像的回调函数
% 读入一张图片
file = "D:\MATLAB files\GUI learn\xiye.jpeg";
im = imread(file,'jpeg');
imshow(im,'Parent',app.UIAxes)
axis(app.UIAxes,'off')
四、点击按钮显示对话框
五、提问对话框
choice = questdlg("您要关闭吗?","关闭","Yes","No","No");
switch choice
case "Yes"
delete(app.UIFigure); % close all window
return; % 返回
case "No"
return;
end
六、交互性操作获得文件
[filename,pathname] = uigetfile({'*.jpg';'*.jpeg';'*.png';'*.*'},'选择图像');
if isequal(filename,0) || isequal(pathname,0) %判断两个值是否相等
errordlg("没有选中文件","错误");
else
%记录获取文件的名字和路径
file = strcat(pathname,filename);
end
[filename,pathname] = uigetfile({'*.jpg';'*.jpeg';'*.png';'*.*'},'选择图像');
if isequal(filename,0) || isequal(pathname,0) %判断两个值是否相等
errordlg("没有选中文件","错误");
else
%记录获取文件的名字和路径
file = strcat(pathname,filename);
end
% 原图
im = imread(file);
imshow(im,'Parent',app.UIAxes);
msgbox("读取成功","提示");
% 灰度图
gray_image = rgb2gray(im);
imshow(gray_image,'Parent',app.UIAxes2);
七、让界面一开始就显示东西
function startupFcn(app)
im = imread("D:\MATLAB files\GUI learn\start.jpeg");
imshow(im,'Parent',app.UIAxes);
end
八、添加函数
methods (Access = private)
function image_show(app)
im = imread("D:\MATLAB files\GUI learn\start.jpeg");
imshow(im,'Parent',app.UIAxes);
end
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
image_show(app);
end
end
九、仪表盘
旋钮的changing方式,鼠标旋转到哪儿,示数到哪儿
changed模式,释放鼠标后,才会示值。
这里的仪表和旋钮不同,仅是显示控件,没有回调函数。
十、强制类型转换
% Callbacks that handle component events
methods (Access = private)
% Value changed function: DatePicker
function DatePickerValueChanged(app, event)
value = app.DatePicker.Value;
% 开始赋值
value = string(value); %强制转换
app.TextArea.Value = value;
end
end
十一、下拉菜单
methods (Access = private)
function func(app,value)
x = 1:0.1:10;
if value == "Option 1"
y = sin(x);
elseif value == "Option 2"
y = cos(x);
else
return;
end
plot(app.UIAxes,x,y)
end
end
% Callbacks that handle component events
methods (Access = private)
% Button down function: UIAxes
function UIAxesButtonDown(app, event)
end
% Value changed function: DropDown
function DropDownValueChanged(app, event)
value = app.DropDown.Value;
if value == "Option 1"
func(app,value);
elseif value == "Option 2"
func(app,value);
else
return;
end
end
end
十三、不把参数写死,可以调整
methods (Access = private)
% Value changing function: Spinner
function SpinnerValueChanging(app, event)
changingValue = event.Value;
app.Gauge.Value = changingValue;
x = 0:0.1:changingValue;
y = sin(x);
plot(app.UIAxes,x,y,'r');
end
end
十四、单选按钮
% Callbacks that handle component events
methods (Access = private)
% Selection changed function: ButtonGroup
function ButtonGroupSelectionChanged(app, event)
selectedButton = app.ButtonGroup.SelectedObject;
switch selectedButton.Text
case '1'
app.Lamp.Color = 'g';
app.Gauge.Value = 1;
case '2'
app.Lamp.Color = 'r';
app.Gauge.Value = 2;
otherwise
app.Lamp.Color = 'g';
app.Gauge.Value = 0;
end
end
end
十五、复选框
methods (Access = private)
% Value changed function: CheckBox
function CheckBoxValueChanged(app, event)
value = app.CheckBox.Value;
% 判断类型
app.EditField.Value = class(value); % 布尔类型
if value == true
app.Lamp.Color = 'r';
elseif value == false
app.Lamp.Color = 'b';
else
app.Lamp.Color = 'g';
end
end
end
十六、读取EXCEL表格
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: Button
function ButtonPushed(app, event)
%读取表格
t = readtable("D:\transmittance_Si.xls");
app.UITable.Data = t;
% 把表头复制上去
app.UITable.ColumnName = t.Properties.VariableNames;
end
end
并绘图
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: Button
function ButtonPushed(app, event)
%读取表格
t = readtable("D:\transmittance_Si.xls");
app.UITable.Data = t;
% 显示表头
%app.UITable.ColumnName = t.Properties.VariableNames; % 显示原有表头
app.EditField.Value = t.Properties.VariableNames{1};
app.EditField_2.Value = t.Properties.VariableNames{2};
% 修改表头
%app.UITable.ColumnName = {'x','y'}; % cell类型
t.Properties.VariableNames{1} = 'x';
t.Properties.VariableNames{2} = 'y';
% 绘图
x = t.x;
y = t.y;
plot(app.UIAxes,x,y,'r')
end
end
十七、增加表格
methods (Access = private)
% Button pushed function: add
function addPushed(app, event)
data1 = app.EditField.Value;
price = app.EditField_2.Value;
nr = {data1 price}; % 组合成cell类型
app.UITable.Data = [app.UITable.Data;nr];
end
end
十八、表格删除
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: Button
function ButtonPushed(app, event)
t = readtable("D:\MATLAB files\Micropolarizer array\FDTD\transmittance_Si.xls");
app.UITable.Data = t;
end
% Button pushed function: Button_2
function Button_2Pushed(app, event)
%(a,b) a代表行,b代表列
row_n = app.EditField.Value;
app.UITable.Data(row_n,:) = [];
end
end