APP designer代码,matlab2021b版本
对于一个简单的电力系统,制作一个gui界面控制发生某种短路后的电流电压波形
其中仿真文件名为sima2.slx,也可以自己改名字或加载自己所需要的内容。
simulink电路图
运行界面
运行结果
当只是bc相短路时,具体细节
要将app文件和仿真文件放一起
classdef app1 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
CCheckBox_2 matlab.ui.control.CheckBox
BCheckBox_2 matlab.ui.control.CheckBox
ACheckBox_2 matlab.ui.control.CheckBox
Label_6 matlab.ui.control.Label
Label_5 matlab.ui.control.Label
Label_4 matlab.ui.control.Label
Button_3 matlab.ui.control.Button
Slider matlab.ui.control.Slider
fangzhenshijian matlab.ui.control.NumericEditField
Label_2 matlab.ui.control.Label
EditField_2 matlab.ui.control.EditField
Label_3 matlab.ui.control.Label
ACheckBox matlab.ui.control.CheckBox
BCheckBox matlab.ui.control.CheckBox
CCheckBox matlab.ui.control.CheckBox
FaultBCheckBox matlab.ui.control.CheckBox
FaultACheckBox matlab.ui.control.CheckBox
FaultGroundCheckBox matlab.ui.control.CheckBox
FaultCCheckBox matlab.ui.control.CheckBox
Button_2 matlab.ui.control.Button
Button matlab.ui.control.Button
TabGroup2 matlab.ui.container.TabGroup
Tab_2 matlab.ui.container.Tab
UIAxes matlab.ui.control.UIAxes
Tab_3 matlab.ui.container.Tab
UIAxes_2 matlab.ui.control.UIAxes
end
properties (Access = private)
simdata % 仿真数据
end
% Callbacks that handle component events
methods (Access = private)
% Value changed function: FaultACheckBox
function FaultACheckBoxValueChanged(app, event)
value=app.FaultACheckBox.Value;%获得复选框的逻辑值,勾上是ture或者false
if value%确定勾上后将三相错误设置勾上
set_param('sima2/Three-Phase Fault','FaultA','on');
else
set_param('sima2/Three-Phase Fault','FaultA','off');
end
end
% Value changed function: FaultCCheckBox
function FaultCCheckBoxValueChanged(app, event)
value=app.FaultCCheckBox.Value;%获得复选框的逻辑值,勾上是ture或者false
if value%确定勾上后将三相错误设置勾上
set_param('sima2/Three-Phase Fault','FaultC','on');%设置成on状态,即勾选状态
else
set_param('sima2/Three-Phase Fault','FaultC','off');
end
end
% Value changed function: FaultBCheckBox
function FaultBCheckBoxValueChanged(app, event)
value=app.FaultBCheckBox.Value;%获得复选框的逻辑值,勾上是ture或者false
if value%确定勾上后将三相错误设置勾上
set_param('sima2/Three-Phase Fault','FaultB','on');
else
set_param('sima2/Three-Phase Fault','FaultB','off');
end
end
% Button pushed function: Button
function ButtonPushed(app, event)
% 设置好每项设置
close all;%将上次未关闭的消息框关闭
waitbox= waitbar(0.1,'仿真运行中...');
app.simdata= sim("sima2",'stoptime',num2str(app.Slider.Value));%运行模型并设定仿真时间
waitbar(0.85,waitbox,'仿真中...')%提示是否正在进行仿真
x=app.simdata.Isimout.time;
y1=app.simdata.Isimout.signals.values(:,1);
y2=app.simdata.Isimout.signals.values(:,2);
y3=app.simdata.Isimout.signals.values(:,3);
v1=app.simdata.Vsimout.signals.values(:,1);
v2=app.simdata.Vsimout.signals.values(:,2);
v3=app.simdata.Vsimout.signals.values(:,3);%电流和电压数据
if app.ACheckBox.Value
va='on';
else
va='off';
end
if app.BCheckBox.Value
vb='on';
else
vb='off';
end
if app.CCheckBox.Value
vc='on';
else
vc='off';
end
%%
% 电压绘图区
cla(app.UIAxes_2);%清除之前绘图,避免bug
plot(app.UIAxes_2,x,v1,Visible=va,Color='blue',LineWidth=2);
hold(app.UIAxes_2,'on');%在该绘图区继续绘图
plot(app.UIAxes_2,x,v2,Visible=vb,Color='g',LineWidth=2);
hold(app.UIAxes_2,'on');
plot(app.UIAxes_2,x,v3,Visible=vc,Color='r',LineWidth=2);
xlabel(app.UIAxes_2,'时间/s');
ylabel(app.UIAxes_2,'电压大小/V')
legend(app.UIAxes_2, 'A相线电压', 'B相线电压','C相线电压');
grid(app.UIAxes_2,"on");%开启网格
% 电流绘图区
cla(app.UIAxes);%清除之前绘图
plot(app.UIAxes,x,y1,Visible=va,Color='blue',LineWidth=2);
hold(app.UIAxes,'on');
plot(app.UIAxes,x,y2,Visible=vb,Color='g',LineWidth=2);
hold(app.UIAxes,'on');
plot(app.UIAxes,x,y3,Visible=vc,Color='r',LineWidth=2);
xlabel(app.UIAxes,'时间/s');
ylabel(app.UIAxes,'电流大小/A')
legend(app.UIAxes, 'A项电流', 'B项电流','C项电流');
grid(app.UIAxes,"on");
%%
waitbar(1,waitbox,'仿真完成')
close all;%关闭等待框
msgbox('仿真完成','消息');
end
% Value changed function: FaultGroundCheckBox
function FaultGroundCheckBoxValueChanged(app, event)
value=app.FaultGroundCheckBox.Value;%获得复选框的逻辑值,勾上是ture或者false
if value%确定勾上后将三相错误设置勾上
set_param('sima2/Three-Phase Fault','GroundFault','on');
else
set_param('sima2/Three-Phase Fault','GroundFault','off');
end
end
% Value changed function: Slider
function SliderValueChanged(app, event)
app.fangzhenshijian.Value=app.Slider.Value;
end
% Value changed function: fangzhenshijian
function fangzhenshijianValueChanged(app, event)
app.Slider.Value=app.fangzhenshijian.Value;
end
% Button pushed function: Button_2
function Button_2Pushed(app, event)
%% 初始化默认值
set_param('sima2/Three-Phase Fault','GroundFault','off');
set_param('sima2/Three-Phase Fault','FaultB','off');
set_param('sima2/Three-Phase Fault','FaultA','off');
set_param('sima2/Three-Phase Fault','FaultC','off');
set_param('sima2/Subsystem/A','Value','1');
set_param('sima2/Subsystem/B','Value','1');
set_param('sima2/Subsystem/C','Value','1');
app.Slider.Value=5;
app.fangzhenshijian.Value=5;
app.FaultGroundCheckBox.Value=false;
app.FaultBCheckBox.Value=false;
app.FaultCCheckBox.Value=false;
app.FaultACheckBox.Value=false;
app.ACheckBox.Value=true;
app.BCheckBox.Value=true;
app.CCheckBox.Value=true;
app.ACheckBox_2.Value=false;
app.BCheckBox_2.Value=false;
app.CCheckBox_2.Value=false;
end
% Value changing function: EditField_2
function EditField_2ValueChanging(app, event)
changingValue = event.Value;
set_param('sima2/Three-Phase Fault','SwitchTimes',changingValue);
set_param('sima2/Subsystem/Constant','Value',changingValue);
%设置三相故障模块(Three-Phase Fault)的开启关闭时间
end
% Button pushed function: Button_3
function Button_3Pushed(app, event)
run("sima2");%将仿真文件与app代码放在同一文件夹内
end
% Value changed function: ACheckBox_2
function ACheckBox_2ValueChanged(app, event)
value = app.ACheckBox_2.Value;
if value
set_param('sima2/Subsystem/A','Value','0');
else
set_param('sima2/Subsystem/A','Value','1');
end
end
% Value changed function: BCheckBox_2
function BCheckBox_2ValueChanged(app, event)
value = app.BCheckBox_2.Value;
if value
set_param('sima2/Subsystem/B','Value','0');
else
set_param('sima2/Subsystem/B','Value','1');
end
end
% Value changed function: CCheckBox_2
function CCheckBox_2ValueChanged(app, event)
value = app.CCheckBox_2.Value;
if value
set_param('sima2/Subsystem/C','Value','0');
else
set_param('sima2/Subsystem/C','Value','1');
end
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 697 620];
app.UIFigure.Name = 'MATLAB App';
% Create TabGroup2
app.TabGroup2 = uitabgroup(app.UIFigure);
app.TabGroup2.Position = [1 150 696 471];
% Create Tab_2
app.Tab_2 = uitab(app.TabGroup2);
app.Tab_2.Title = '电流波形';
% Create UIAxes
app.UIAxes = uiaxes(app.Tab_2);
title(app.UIAxes, '电流')
xlabel(app.UIAxes, '时间/s')
ylabel(app.UIAxes, '幅值')
zlabel(app.UIAxes, 'Z')
app.UIAxes.Position = [5 9 689 437];
% Create Tab_3
app.Tab_3 = uitab(app.TabGroup2);
app.Tab_3.Title = '电压波形';
% Create UIAxes_2
app.UIAxes_2 = uiaxes(app.Tab_3);
title(app.UIAxes_2, '电压')
xlabel(app.UIAxes_2, '时间/s')
ylabel(app.UIAxes_2, '幅值')
zlabel(app.UIAxes_2, 'Z')
app.UIAxes_2.Position = [5 9 689 437];
% Create Button
app.Button = uibutton(app.UIFigure, 'push');
app.Button.ButtonPushedFcn = createCallbackFcn(app, @ButtonPushed, true);
app.Button.WordWrap = 'on';
app.Button.BackgroundColor = [1 0 0];
app.Button.FontName = '楷体';
app.Button.FontSize = 34;
app.Button.FontWeight = 'bold';
app.Button.FontColor = [1 1 1];
app.Button.Position = [595 16 100 85];
app.Button.Text = '开始仿真';
% Create Button_2
app.Button_2 = uibutton(app.UIFigure, 'push');
app.Button_2.ButtonPushedFcn = createCallbackFcn(app, @Button_2Pushed, true);
app.Button_2.Position = [595 101 100 24];
app.Button_2.Text = '重置';
% Create FaultCCheckBox
app.FaultCCheckBox = uicheckbox(app.UIFigure);
app.FaultCCheckBox.ValueChangedFcn = createCallbackFcn(app, @FaultCCheckBoxValueChanged, true);
app.FaultCCheckBox.Text = 'FaultC';
app.FaultCCheckBox.Position = [198 111 58 22];
% Create FaultGroundCheckBox
app.FaultGroundCheckBox = uicheckbox(app.UIFigure);
app.FaultGroundCheckBox.ValueChangedFcn = createCallbackFcn(app, @FaultGroundCheckBoxValueChanged, true);
app.FaultGroundCheckBox.Text = 'FaultGround';
app.FaultGroundCheckBox.Position = [255 111 89 22];
% Create FaultACheckBox
app.FaultACheckBox = uicheckbox(app.UIFigure);
app.FaultACheckBox.ValueChangedFcn = createCallbackFcn(app, @FaultACheckBoxValueChanged, true);
app.FaultACheckBox.Text = 'FaultA';
app.FaultACheckBox.Position = [83 111 57 22];
% Create FaultBCheckBox
app.FaultBCheckBox = uicheckbox(app.UIFigure);
app.FaultBCheckBox.ValueChangedFcn = createCallbackFcn(app, @FaultBCheckBoxValueChanged, true);
app.FaultBCheckBox.Text = 'FaultB';
app.FaultBCheckBox.Position = [139 111 57 22];
% Create CCheckBox
app.CCheckBox = uicheckbox(app.UIFigure);
app.CCheckBox.Text = 'C相';
app.CCheckBox.Position = [521 111 43 22];
app.CCheckBox.Value = true;
% Create BCheckBox
app.BCheckBox = uicheckbox(app.UIFigure);
app.BCheckBox.Text = 'B相';
app.BCheckBox.Position = [480 111 42 22];
app.BCheckBox.Value = true;
% Create ACheckBox
app.ACheckBox = uicheckbox(app.UIFigure);
app.ACheckBox.Text = 'A相';
app.ACheckBox.Position = [433 111 42 22];
app.ACheckBox.Value = true;
% Create Label_3
app.Label_3 = uilabel(app.UIFigure);
app.Label_3.HorizontalAlignment = 'right';
app.Label_3.Position = [359 79 65 22];
app.Label_3.Text = '故障时间:';
% Create EditField_2
app.EditField_2 = uieditfield(app.UIFigure, 'text');
app.EditField_2.ValueChangingFcn = createCallbackFcn(app, @EditField_2ValueChanging, true);
app.EditField_2.Position = [438 79 37 22];
app.EditField_2.Value = '[1 2]';
% Create Label_2
app.Label_2 = uilabel(app.UIFigure);
app.Label_2.HorizontalAlignment = 'right';
app.Label_2.Position = [31 38 53 22];
app.Label_2.Text = '仿真时间';
% Create fangzhenshijian
app.fangzhenshijian = uieditfield(app.UIFigure, 'numeric');
app.fangzhenshijian.Limits = [0 20];
app.fangzhenshijian.ValueChangedFcn = createCallbackFcn(app, @fangzhenshijianValueChanged, true);
app.fangzhenshijian.Position = [36 17 48 19];
app.fangzhenshijian.Value = 5;
% Create Slider
app.Slider = uislider(app.UIFigure);
app.Slider.Limits = [0.01 20];
app.Slider.MajorTicks = [0.01 2 4 6 8 10 12 14 16 18 20];
app.Slider.ValueChangedFcn = createCallbackFcn(app, @SliderValueChanged, true);
app.Slider.Position = [105 47 324 3];
app.Slider.Value = 5;
% Create Button_3
app.Button_3 = uibutton(app.UIFigure, 'push');
app.Button_3.ButtonPushedFcn = createCallbackFcn(app, @Button_3Pushed, true);
app.Button_3.Position = [474 26 100 24];
app.Button_3.Text = '查看电路图';
% Create Label_4
app.Label_4 = uilabel(app.UIFigure);
app.Label_4.Position = [364 111 65 22];
app.Label_4.Text = '坐标显示:';
% Create Label_5
app.Label_5 = uilabel(app.UIFigure);
app.Label_5.Position = [19 111 65 22];
app.Label_5.Text = '短路故障:';
% Create Label_6
app.Label_6 = uilabel(app.UIFigure);
app.Label_6.Position = [19 79 65 22];
app.Label_6.Text = '断路故障:';
% Create ACheckBox_2
app.ACheckBox_2 = uicheckbox(app.UIFigure);
app.ACheckBox_2.ValueChangedFcn = createCallbackFcn(app, @ACheckBox_2ValueChanged, true);
app.ACheckBox_2.Text = 'A';
app.ACheckBox_2.Position = [83 79 30 22];
% Create BCheckBox_2
app.BCheckBox_2 = uicheckbox(app.UIFigure);
app.BCheckBox_2.ValueChangedFcn = createCallbackFcn(app, @BCheckBox_2ValueChanged, true);
app.BCheckBox_2.Text = 'B';
app.BCheckBox_2.Position = [115 79 30 22];
% Create CCheckBox_2
app.CCheckBox_2 = uicheckbox(app.UIFigure);
app.CCheckBox_2.ValueChangedFcn = createCallbackFcn(app, @CCheckBox_2ValueChanged, true);
app.CCheckBox_2.Text = 'C';
app.CCheckBox_2.Position = [144 79 31 22];
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = app1
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end