Matlab R2020A - App Designer Lamp Behaving Opposite to code

8 ビュー (過去 30 日間)
Anand Mohan
Anand Mohan 2020 年 8 月 1 日
コメント済み: Anand Mohan 2020 年 8 月 2 日
Hi,
I have designed a test app using AppDesigner. I am using a simple slide switch and lamp to test the slide switch action. The idea is when the switch is set to "On" position the lamp colour changes to "Green". When its toggled back to "OFF" the lamp changes to "Red".
My confusion is the lamp behaves opposite to my expectation. When toggled to "Off" position the lamp colour is "Green" and when the toggle is "On" the lamp lights up as "Red". The same behaviour is noticed when I replace the "app.Lamp.Color" property with "app.Lamp.Enable" property. The lamp behviour is opposite of what I expect.
The relevant code section is located within "Switch2ValueChanged(app, event)" callback function.
classdef lamptest < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
Panel matlab.ui.container.Panel
Switch2Label matlab.ui.control.Label
Switch2 matlab.ui.control.Switch
Lamp matlab.ui.control.Lamp
end
% Callbacks that handle component events
methods (Access = private)
% Value changed function: Switch2
function Switch2ValueChanged(app, event)
value = app.Switch2.Value;
if strcmp(value, "Off")
app.Lamp.Color = [1,0,0];
else
app.Lamp.Color = [0,1,0];
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 338 145];
app.UIFigure.Name = 'MATLAB App';
% Create Panel
app.Panel = uipanel(app.UIFigure);
app.Panel.Title = 'Panel';
app.Panel.Position = [30 18 293 106];
% Create Switch2Label
app.Switch2Label = uilabel(app.Panel);
app.Switch2Label.HorizontalAlignment = 'center';
app.Switch2Label.Position = [60 17 48 22];
app.Switch2Label.Text = 'Switch2';
% Create Switch2
app.Switch2 = uiswitch(app.Panel, 'slider');
app.Switch2.ValueChangedFcn = createCallbackFcn(app, @Switch2ValueChanged, true);
app.Switch2.Position = [61 54 45 20];
% Create Lamp
app.Lamp = uilamp(app.Panel);
app.Lamp.HandleVisibility = 'callback';
app.Lamp.Interruptible = 'off';
app.Lamp.Position = [217 45 20 20];
app.Lamp.Color = [1 0 0];
% 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 = lamptest
% 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
Off position - Lamp Green
On position - Lamp Red
Can I request help to troubleshoot this behaviour?

採用された回答

Image Analyst
Image Analyst 2020 年 8 月 1 日
Is app.Switch2.Value a string, like 'on' or 'off'? If so, try strcmpi() instead of strcmp().
Or is it a logical or number like 0 or 1?
  1 件のコメント
Anand Mohan
Anand Mohan 2020 年 8 月 2 日
Thanks for the response. After a lot of debugging, nothing was fixed. Then restarted the laptop and now its all good. Some issue on the device causing Matlab to behave incorrectly. Thanks for the help. Next time first step would be turn the PC off and then on.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDevelop uifigure-Based Apps についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by