If else conditionals in matlab app.designer?
Show older comments
I have a doubt about conditionals on matlab app.designer.
For example in this litlle and simple app. I have defined a private variable F which value is 60.
The point here is that, when I select the "ac" option in the dropmenu below, and click the button, the number 1917 appears on the white box

This is the conditional I have designed to occur that, and doesn't work. The question is that if the option of the dropmenu is only a letter (for instance 'a' or 'b' or whatever, it does work. But when the option is one or more characters, like in the example below, it doesn`t work and appears the following error.


How it is possible that the code above works perfectly when I have only one letter but does not when are two or more characters?
Thank you very much!
Accepted Answer
More Answers (2)
Cris LaPierre
on 16 Jan 2021
Edited: Cris LaPierre
on 16 Jan 2021
Because your check of the menu value returns a value for each character, not one.
a='ac';
a=='ac'
You can use a single &, and your code will run. Better is to use strcmp, though.
strcmp(a,'ac')
if strcmp(app.menu.Value, 'ac') && ...
or define the values as string with double quotes, not as "cell string", which is a cell of CHAR vectors.
6 Comments
NICOLE MIN
on 21 Apr 2021
i have the same issue her and how can o define and condition and convert to logical scalar value?

Jan
on 21 Apr 2021
@NICOLE MIN: Without seeing the contents of your variables, there is no chance to guess, what you have to change. Usually an all() or any() helps.
NICOLE MIN
on 21 Apr 2021
Alright! Please take a look tomorrow once I screenshot the variables code tmr. Thanks for your help
Jan
on 21 Apr 2021
Avoid posting screenshots but prefere to post code as text. This makes it muh easier to test and write an answer.
NICOLE MIN
on 22 Apr 2021
properties (Access = private)
Age;
Glucose;
BMI;
sysBP;
diaBP; % Description
Diagnosis
Gender
end
methods (Access = private)
end
% Callbacks that handle component events
methods (Access = private)
% Value changed function: AgeEditField
function AgeEditFieldValueChanged(app, event)
value = app.AgeEditField.Value;
app.Age = value;
end
% Value changed function: GlucoseEditField
function GlucoseEditFieldValueChanged(app, event)
value = app.GlucoseEditField.Value;
app.Glucose = value;
end
% Value changed function: BMIEditField
function BMIEditFieldValueChanged(app, event)
value = app.BMIEditField.Value;
app.BMI = value;
end
% Value changed function: sysBPEditField
function sysBPEditFieldValueChanged(app, event)
value = app.sysBPEditField.Value;
app.sysBP = value;
end
% Value changed function: diaBPEditField
function diaBPEditFieldValueChanged(app, event)
value = app.diaBPEditField.Value;
app.diaBP = value;
end
% Button pushed function: PredictButton
function PredictButtonPushed(app, event)
app.Age = 1:1000;% allow user to field any number in editfield from 1 to 1000
app.Glucose=1:1000;
app.BMI=1:1000;
app.sysBP=1:1000;
app.diaBP=1:1000;
high_glucose =350;
low_glucose=0;
mean_Glucosefeat=121;
input_fuzzy_glucose=175;
high_BMI=60;
low_BMI=0;
mean_BMI=39;
input_fuzzy_BMI=30;
max_rule1=175;
max_use1=350;
max_rule2=30;
max_use2=60;
b =app.Glucose;
c= app.BMI;
d=app.sysBP;
e=app.diaBP;
if app.GenderSwitch.Value=="Male"
(b <=max_rule1 && c<max_rule2 && d<35)&&(e<=117.5 && f<=77);
app.Diagnosis='low risk ';
else
app.GenderSwitch.Value="Male";
(b>=max_rule1 && c==max_rule2&&d>35<50)&&(e>=118 && f<=78);
app.Diagnosis='medium risk ';
end
if app.GenderSwitch.Value=="Male"
(b>=max_rule1 && c>max_rule2&&d>51<60)&&(e>=134 && f<=79);
app.Diagnosis=' risky ';
end
if app.GenderSwitch.Value=="Male"
(b==max_rule1 && b<=max_rule2&&d<35)&&(e<=117.5 && f<=77);
app.Diagnosis='medium risk ';
else
app.GenderSwitch.Value="Male";
(b >=maxrule1 && c>=max_rule2&&d>35<50)&&(e<=117.5 && f<=77);
app.Diagnosis='risky ';
end
if app.GenderSwitch.Value=="Male"
(b >=max_rule1 && c>max_use2&&d>51<60)&&(e<=117.5 && f<=77);
app.Diagnosis='high risk ';
end
if app.GenderSwitch.Value=="Male"
(b==max_rule1 && c<=max_rule2&&d<35)&&(e<=117.5 && f<=77);
app.Diagnosis='risky ';
else
app.GenderSwitch.Value="Male";
(b>=maxrule1 &&c>=max_rule2 && d>35<50)&&(e<=117.5 && f<=77);
app.Diagnosis='risky ';
end
if app.GenderSwitch.Value=="Male"
(b>=max_use1 && c>max_use2 && d>51<60)&&(e<=117.5 && f<=77);
app.Diagnosis='high risk ';
end
if app.GenderSwitch.Value== "Male"
(b==max_rule1 && c<=max_rule2 && d<35)&&(e<=117.5 && f<=77);
app.Diagnosis='risky ';
else
app.GenderSwitch.Value= "Male";
(b >=maxrule1 && c>=max_rule2&& d>35<50)&&(e<=117.5 && f<=77);
app.Diagnosis='risky ';
end
if app.GenderSwitch.Value== "Male"
(b>=max_use1 && c>max_use2&& d>51<60)&&(e<=117.5 && f<=77);
app.Diagnosis='high risk ';
else
app.GenderSwitch.Value= 'Male';
(app.Glucose<=max_rule1 && app.BMI<max_rule2)&&(app.sysBP<=117.5 && app.diaBP<=77);
app.Diagnosis='normal ';
if app.GenderSwitch.Value== "Male"
(app.Glucose >207 && app.Age>60);
app.Diagnosis='danger ';
end
if app.GenderSwitch.Value=="Female"
(b <=max_rule1 && c<max_rule2&& d<35)&&(e<=113 && f<=71);
app.Diagnosis='low risk ';
else
app.GenderSwitch.Value="Female";
(b>=max_rule1 && c==max_rule2 && d>35<50)&&(e>=113 && f<=73);
app.Diagnosis='medium risk ';
end
if app.GenderSwitch.Value== "Female"
(b>=max_rule1 && c>max_rule2 && d<51<60)&&(e>=127 && f<=76);
app.EditField=' risky ';
end
if app.GenderSwitch.Value== "Female"
(b==max_rule1 && c<=max_rule2 && d<35)&&(e<=113 && f<=71);
app.Diagnosis='medium risk ';
else
app.GenderSwitch.Value= "Female";
(app.Glucose >=maxrule1 && app.BMI>=max_rule2 && app.Age>35<50)&&(app.sysBP<=113 && app.diaBP<=73);
app.Diagnosis='risky ';
end
if app.GenderSwitch.Value== "Female"
(b>=max_rule1 && c>max_use2&& d>51<60)&&(e<=127 && f<=76);
app.Diagnosis='high risk ';
end
if app.GenderSwitch.Value== "Female"
(b==max_rule1 && c<=max_rule2 && d<35)&&(e<=113 && f<=71);
app.Diagnosis='risky ';
else
app.GenderSwitch.Value= "Female";
(b>=maxrule1 && c>=max_rule2 && d>35<50)&&(e<=113 && f<=73);
app.Diagnosis='risky ';
end
if app.GenderSwitch.Value== "Female"
(b>=max_use1 && c>max_use2&& d>51<60)&&(e<=127 && f<=76);
app.Diagnosis='high risk ';
else
app.GenderSwitch.Value= "Female";
(b<=max_rule1 && c<max_rule2) &&(d<=113 && e<=71);
app.Diagnosis='normal';
end
if app.GenderSwitch.Value== "Female"
(b>207 && app.Age>60);
app.Diagnosis='danger ' ;
else
app.Glucose=[];
app.BMI=[];
app.sysBP=[];
app.diaBP=[];
app.Age=[];
app.Diagnosis=('Not Applicable');
end
end
% Value changed function: DiagnosisTextArea
function DiagnosisTextAreaValueChanged(app, event)
value = app.DiagnosisTextArea.Value;
app.Diagnosis = value;
end
% Value changed function: GenderSwitch
function GenderSwitchValueChanged(app, event)
value = app.GenderSwitch.Value;
if value=="Female"
app.PregnantCheckBox='on';
app.GenderSwitch.Enable = 'on';
else
end
if value=="Male"
app.Pregnant.CheckBox="off"
% Disable the switches and buttons if they were on
app.GenderSwitch.Enable = 'off';
end
end
% Value changed function: mgdlButton
function mgdlButtonValueChanged(app, event)
value = app.mgdlButton.Value;
value=app.GlucoseEditField.Value*18;
app.Glucose=value;
end
please help ! thanks
NICOLE MIN
on 22 Apr 2021
this is a layout of my app.

Categories
Find more on Develop Apps Using App Designer in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!