If else conditionals in matlab app.designer?

54 views (last 30 days)
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

Steven Lord
Steven Lord on 16 Jan 2021
Compare:
'ac' == 'ad' % returns a 1-by-2 logical array
ans = 1x2 logical array
1 0
'ac' == "ad" % returns a 1-by-1 logical array
ans = logical
0
When you compare two char arrays using ==, the resulting logical array is the same size as the char arrays (or the size of the non-scalar, if one is scalar, or the compatible size if you're using a release that supports implicit expansion and they are compatibly sized.)
When you compare a char array and a scalar string the char array is converted into a string (in my second line of code above, the two character char vector 'ac' becomes the scalar string "ac") and then we compare the string arrays.
If using strings is not an option, use strcmp or one of the functions given in the "See Also" section of the documentation page for the strcmp function.

More Answers (2)

Cris LaPierre
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'
ans = 1x2 logical array
1 1
You can use a single &, and your code will run. Better is to use strcmp, though.
strcmp(a,'ac')
ans = logical
1

Jan
Jan on 16 Jan 2021
Edited: Jan on 16 Jan 2021
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
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

Sign in to comment.

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!