|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
本帖最后由 sharkN 于 2020-12-14 18:26 编辑 6 q' T& z+ t0 v7 h
) `2 ^$ N0 U$ U( Q) \1、我在MATLAB的app designer模块设计一个简单的控制界面,有三盏灯,分别用不同开关控制它的颜色变化和频率:
3 G3 w1 H7 i/ g3 G( h* \ 1)switch1就是左右切换lamp1的颜色;2 E L; b' {/ o& Q2 ~
2)switch2是切换“On”后闪烁频率为0.5秒的颜色切换;
9 ?( P) e# G* I2 {1 Q 上面两个我之前已经运行成功了 }) }- ^& X! g6 R$ X) L: U) a
3)lamp3复杂一点,通过输入读取三个值:Temperature(温度)、Humidity(湿度)、WinDSPeed(风速)来列式计算出HCI(人体舒适度指数),然后在半圆形仪表(HCI)中显示出来,再用if判定一下等级在圆形仪表(HCI_Grade)中显示,若满足相应等级,灯就变色。
. u; O4 _/ i1 A9 T4 L8 e0 P& s' O' q- A0 b* D
MATLAB版本是64位R2016b,操作系统是64位Windows8。请教大神,非常感谢!
. |: _5 j% E( V 界面如下:
, e/ y/ t- S; |; {5 h, U
) N5 _3 ^- @0 v' b) `
$ F! Z& X2 G5 G; x
2、完整代码如下:
( C( f4 _7 X1 ], L6 l' r1 ?4 M- classdef App2 < matlab.apps.AppBase
- % Properties that correspond to app components
- properties (Access = public)
- UIFigure matlab.ui.Figure
- Lamp2Label matlab.ui.control.Label
- Lamp2 matlab.ui.control.Lamp
- Switch2Label matlab.ui.control.Label
- Switch2 matlab.ui.control.Switch
- Panel matlab.ui.container.Panel
- PMVPanel matlab.ui.container.Panel
- Lamp3Label matlab.ui.control.Label
- Lamp3 matlab.ui.control.Lamp
- Label matlab.ui.control.Label
- Temperature matlab.ui.control.NumericEditField
- Label_2 matlab.ui.control.Label
- Humidity matlab.ui.control.Slider
- msKnobLabel matlab.ui.control.Label
- WindSpeed matlab.ui.control.Knob
- Label_4 matlab.ui.control.Label
- HCI_Grade matlab.ui.control.Gauge
- Button matlab.ui.control.Button
- Label_3 matlab.ui.control.Label
- HCI matlab.ui.control.SemicircularGauge
- Lamp1Label matlab.ui.control.Label
- Lamp1 matlab.ui.control.Lamp
- Switch1Label matlab.ui.control.Label
- Switch1 matlab.ui.control.RockerSwitch
- end
- properties (Access = private)
- HCI
- HCI_Grade
- end
- methods (Access = private)
- % Code that executes after component creation
- function startupFcn(app)
- app.Lamp1.Color = 'black';
- app.Lamp2.Color = 'black';
- app.Lamp3.Color = 'black';
- app.Temperature.Value = 0;
- app.Humidity.Value = 0;
- app.WindSpeed.Value = 0;
- app.HCI.Value = 0;
- app.HCI_Grade.Value = 0;
- end
- % Value changed function: Switch2
- function Switch2ValueChanged(app, event)
- value = app.Switch2.Value;
- if strcmp(value,'0')
- app.Lamp2.Color = 'black';
- value = '1';
- else
- i = 0;
- while(i<10)
- app.Lamp2.Color = 'green';
- pause(0.5);
- app.Lamp2.Color = 'red';
- pause(0.5);
- i = i+1;
- end
- value = '0';
- end
- end
- % Value changed function: Switch1
- function Switch1ValueChanged(app, event)
- value = app.Switch1.Value;
- if strcmp(value,'0');
- app.Lamp1.Color = 'black';
- value = '1';
- else
- app.Lamp1.Color = 'yellow';
- value = '0';
- end
- end
- % Button pushed function: Button
- function ButtonPushed(app, event)
- t = app.Temperature.Value;
- f = app.Humidity.Value;
- v = app.WindSpeed.Value;
- ssd = (1.818*t+18.18)*(0.88+0.002*(f/100))+(t-32)/(45-t)-3.2*v+18.2;
- app.HCI.Value = ssd;
- if ssd<25
- app.HCI_Grade.Value = -4;
- else if ssd<38
- app.HCI_Grade.Value = -3;
- else if ssd<50
- app.HCI_Grade.Value = -2;
- else if ssd<58
- app.HCI_Grade.Value = -1;
- else if ssd<70
- app.HCI_Grade.Value = 0;
- else if ssd<75
- app.HCI_Grade.Value = 1;
- else if ssd<79
- app.HCI_Grade.Value = 2;
- else if ssd<85
- app.HCI_Grade.Value = 3;
- else
- app.HCI_Grade.Value = 4;
- end
- value = app.HCI_Grade.Value;
- if (value>-2)&&(value<2)
- app.Lamp3.Color = 'green';
- else
- app.Lamp3.Color = 'red';
- end
- end
- end
- % App initialization and construction
- methods (Access = private)
- % Create UIFigure and components
- function createComponents(app)
- % Create UIFigure
- app.UIFigure = uifigure;
- app.UIFigure.Position = [100 100 814 621];
- app.UIFigure.Name = 'UI Figure';
- setAutoResize(app, app.UIFigure, true)
- % Create Lamp2Label
- app.Lamp2Label = uilabel(app.UIFigure);
- app.Lamp2Label.HorizontalAlignment = 'right';
- app.Lamp2Label.FontWeight = 'bold';
- app.Lamp2Label.FontAngle = 'italic';
- app.Lamp2Label.Position = [43 425 44 15];
- app.Lamp2Label.Text = 'Lamp2';
- % Create Lamp2
- app.Lamp2 = uilamp(app.UIFigure);
- app.Lamp2.Position = [102 422 20 20];
- % Create Switch2Label
- app.Switch2Label = uilabel(app.UIFigure);
- app.Switch2Label.HorizontalAlignment = 'center';
- app.Switch2Label.FontWeight = 'bold';
- app.Switch2Label.FontAngle = 'italic';
- app.Switch2Label.Position = [172.5 392 50 15];
- app.Switch2Label.Text = 'Switch2';
- % Create Switch2
- app.Switch2 = uiswitch(app.UIFigure, 'slider');
- app.Switch2.ItemsData = {'0', '1'};
- app.Switch2.ValueChangedFcn = createCallbackFcn(app, @Switch2ValueChanged, true);
- app.Switch2.FontWeight = 'bold';
- app.Switch2.FontAngle = 'italic';
- app.Switch2.Position = [175 422 45 20];
- app.Switch2.Value = '0';
- % Create Panel
- app.Panel = uipanel(app.UIFigure);
- app.Panel.Title = 'Panel';
- app.Panel.Position = [-434 252 260 221];
- % Create PMVPanel
- app.PMVPanel = uipanel(app.UIFigure);
- app.PMVPanel.TitlePosition = 'centertop';
- app.PMVPanel.Title = 'PMV';
- app.PMVPanel.BackgroundColor = [0.9686 0.9176 0.9176];
- app.PMVPanel.Position = [289 175 482 422];
- % Create Lamp3Label
- app.Lamp3Label = uilabel(app.PMVPanel);
- app.Lamp3Label.HorizontalAlignment = 'right';
- app.Lamp3Label.FontWeight = 'bold';
- app.Lamp3Label.FontAngle = 'italic';
- app.Lamp3Label.Position = [127 31 44 15];
- app.Lamp3Label.Text = 'Lamp3';
- % Create Lamp3
- app.Lamp3 = uilamp(app.PMVPanel);
- app.Lamp3.Position = [186 28 20 20];
- % Create Label
- app.Label = uilabel(app.PMVPanel);
- app.Label.HorizontalAlignment = 'right';
- app.Label.FontWeight = 'bold';
- app.Label.Position = [25 364 96 15];
- app.Label.Text = '平均气温(℃)';
- % Create Temperature
- app.Temperature = uieditfield(app.PMVPanel, 'numeric');
- app.Temperature.Limits = [0 40];
- app.Temperature.ValueDisplayFormat = '%.2f';
- app.Temperature.HorizontalAlignment = 'center';
- app.Temperature.Position = [136 360 110 22];
- % Create Label_2
- app.Label_2 = uilabel(app.PMVPanel);
- app.Label_2.HorizontalAlignment = 'right';
- app.Label_2.FontWeight = 'bold';
- app.Label_2.Position = [25 299 94 15];
- app.Label_2.Text = '相对湿度(%)';
- % Create Humidity
- app.Humidity = uislider(app.PMVPanel);
- app.Humidity.FontWeight = 'bold';
- app.Humidity.FontAngle = 'italic';
- app.Humidity.Position = [132 316 124 3];
- % Create msKnobLabel
- app.msKnobLabel = uilabel(app.PMVPanel);
- app.msKnobLabel.HorizontalAlignment = 'center';
- app.msKnobLabel.FontWeight = 'bold';
- app.msKnobLabel.Position = [337 247 78 15];
- app.msKnobLabel.Text = '风速(m/s)';
- % Create WindSpeed
- app.WindSpeed = uiknob(app.PMVPanel, 'continuous');
- app.WindSpeed.Limits = [0 20];
- app.WindSpeed.FontWeight = 'bold';
- app.WindSpeed.FontAngle = 'italic';
- app.WindSpeed.Position = [336 296 81 81];
- % Create Label_4
- app.Label_4 = uilabel(app.PMVPanel);
- app.Label_4.HorizontalAlignment = 'center';
- app.Label_4.FontWeight = 'bold';
- app.Label_4.Position = [296 90 122 15];
- app.Label_4.Text = '人体舒适度指数分级';
- % Create HCI_Grade
- app.HCI_Grade = uigauge(app.PMVPanel, 'circular');
- app.HCI_Grade.Limits = [-4 4];
- app.HCI_Grade.FontWeight = 'bold';
- app.HCI_Grade.FontAngle = 'italic';
- app.HCI_Grade.Position = [303 120 108 108];
- % Create Button
- app.Button = uibutton(app.PMVPanel, 'push');
- app.Button.ButtonPushedFcn = createCallbackFcn(app, @ButtonPushed, true);
- app.Button.BackgroundColor = [1 1 0];
- app.Button.FontWeight = 'bold';
- app.Button.Position = [288 22 92 32];
- app.Button.Text = '按钮';
- % Create Label_3
- app.Label_3 = uilabel(app.PMVPanel);
- app.Label_3.HorizontalAlignment = 'center';
- app.Label_3.FontWeight = 'bold';
- app.Label_3.Position = [104 104 96 15];
- app.Label_3.Text = '人体舒适度指数';
- % Create HCI
- app.HCI = uigauge(app.PMVPanel, 'semicircular');
- app.HCI.Position = [65 134 164 89];
- % Create Lamp1Label
- app.Lamp1Label = uilabel(app.UIFigure);
- app.Lamp1Label.HorizontalAlignment = 'right';
- app.Lamp1Label.FontWeight = 'bold';
- app.Lamp1Label.FontAngle = 'italic';
- app.Lamp1Label.Position = [43 540 44 15];
- app.Lamp1Label.Text = 'Lamp1';
- % Create Lamp1
- app.Lamp1 = uilamp(app.UIFigure);
- app.Lamp1.Position = [102 537 20 20];
- % Create Switch1Label
- app.Switch1Label = uilabel(app.UIFigure);
- app.Switch1Label.HorizontalAlignment = 'center';
- app.Switch1Label.FontWeight = 'bold';
- app.Switch1Label.FontAngle = 'italic';
- app.Switch1Label.Position = [172 485 50 15];
- app.Switch1Label.Text = 'Switch1';
- % Create Switch1
- app.Switch1 = uiswitch(app.UIFigure, 'rocker');
- app.Switch1.ItemsData = {'0', '1'};
- app.Switch1.ValueChangedFcn = createCallbackFcn(app, @Switch1ValueChanged, true);
- app.Switch1.FontWeight = 'bold';
- app.Switch1.FontAngle = 'italic';
- app.Switch1.Position = [184 524 20 45];
- app.Switch1.Value = '0';
- end
- end
- methods (Access = public)
- % Construct app
- function app = App2()
- % Create and configure components
- createComponents(app)
- % Register the app with App Designer
- registerApp(app, app.UIFigure)
- % Execute the startup function
- runStartupFcn(app, @startupFcn)
- 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
# S; Y9 F' [) O( f6 k2 A/ z1 j 8 z) M) l3 B2 ?
9 L3 {4 [, }! m6 {( v: `4 }/ L, w( g1 A* P! c4 G
3、出错代码如下:
. m; `- S1 k2 [2 f4 A! E1 D2 Z- % App initialization and construction
- methods (Access = private)
4 ~; i+ _, n7 E9 r" u: Y $ I8 |, N; |; M6 n+ F
$ R0 \9 D3 G# ?
1 k0 m% N0 t% n
可是这部分代码是灰色区域的,是不可编辑修改的,如下图。我该怎么消除错误?是不是添加的属性有问题?
- |$ K5 }+ Q1 j+ [0 A1 S
8 Z: w2 K# r0 v) m. f
! ?: u- F7 A8 Y7 G# U; a
8 h: A: E$ D) @' i( y1 H% E$ |: ?7 {
5 t: b2 q5 ^7 B/ W | 8 s$ L$ o3 U* _/ R
|
|