why is the lamp changing color?

조회 수: 2 (최근 30일)
Muazma Ali
Muazma Ali 2023년 6월 4일
댓글: Muazma Ali 2023년 6월 5일
Hi
The lamp in this short app is not supposed to change color when something is typed in the first field ( Nr of zones) because the criterion I have is related to the second field (Nr Of salts).
Can anybody check and tell me , I have tried to put a condition so that it is supposed to show magenta when the first field (Nr of Zones) is empty . I dont know how to program the app so it lamp color does not change from magenta to red when the user just has typed in Nr of Zones and not reached to the second field (Nr of Salts)

채택된 답변

VBBV
VBBV 2023년 6월 5일
편집: VBBV 2023년 6월 5일
Hi Mauzmi Ali ,
In the app designer code, the editField component accepts numeric inputs. By default the Matlab editField component assigns a value 0, Hence you need to change the code as shown below with additional conditions added in the if statement.
if ~isempty(app.antall_soner)
if (app.NrofsaltsEditField.Value>3) || (app.NrofsaltsEditField.Value>=1 && app.NrofsaltsEditField.Value<2)
app.Lamp.Color='red';
app.EditField.Value='Choose minimum 2 salts and maximum 3 salts';
% if the user inputs salts without number of zone inputs
elseif (app.NrofsaltsEditField.Value==3||app.NrofsaltsEditField.Value==2) && (app.NrofzonesEditField.Value == 0)
app.Lamp.Color='red';
app.EditField.Value='Please enter Number of Zones';
elseif (app.NrofsaltsEditField.Value==3||app.NrofsaltsEditField.Value==2) && (app.NrofzonesEditField.Value ~= 0)
app.Lamp.Color='green';
app.EditField.Value='Input accepted!';
end
app.nr_zones_analyzed=app.nr_zones_analyzed+1;
end
  댓글 수: 2
VBBV
VBBV 2023년 6월 5일
편집: VBBV 2023년 6월 5일
The lamp is changing color due to presence of default value of 0 in the edit field related to app.NrofsaltsEditField.Value If you observe the if-condition thats present in your code (shown below), it will always satisfy the condition, because edit field value is 0 which is less than 2
if (app.NrofsaltsEditField.Value>3) || app.NrofsaltsEditField.Value<2)
Hence, you need to modify it as below
if (app.NrofsaltsEditField.Value>3) || (app.NrofsaltsEditField.Value>=1 && app.NrofsaltsEditField.Value<2)
Muazma Ali
Muazma Ali 2023년 6월 5일
@VBBV thanks a lot for your help. Now I understand what I did wrong :)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2023년 6월 4일
In Numer_of_zonesEditFieldValueChanged you have
app.Lamp.Color='r';
and
app.Lamp.Color='green';
so it's quite possible that changing the number of zones will change the lamp color.
The field cannot be empty. Your app does not allow it. It must have something there so you don't need to check for the empty condition.

카테고리

Help CenterFile Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by