App Designer- How do I get my drop down box to output to Edit Field boxes?

10 views (last 30 days)
I can't figure out how to get my drop down boxes to work for multiple options. Only the code with 1 value works. I know I have tried more variations but I'm frustrated and these are the only 2 I remember right now.
The error I got with "if, elseif, else" was "matrix dimensions must agree" after running the app and selecting the second or third options but the first still works.
The app.A125_1, etc are the Edit Field (Numeric) boxes that I am setting the input for.
Working code: % Value changed function: Material2DropDown function Material2DropDownValueChanged(app, event) value = app.Material2DropDown.Value
app.Mat2.Value = app.Material2DropDown.Value;
if value == 'Brick, unglazed'
app.A125_2.Value = 0.03
app.A250_2.Value = 0.03
app.A500_2.Value = 0.03
app.A1000_2.Value = 0.04
app.A2000_2.Value = 0.05
app.A4000_2.Value = 0.07;
end
end
First failed attempt: % Value changed function: Material1DropDown function Material1DropDownValueChanged(app, event) value = app.Material1DropDown.Value app.Mat1.Value = app.Material1DropDown.Value;
if value == 'Brick, unglazed'
app.A125_1.Value = 0.03
app.A250_1.Value = 0.03
app.A500_1.Value = 0.03
app.A1000_1.Value = 0.04
app.A2000_1.Value = 0.05
app.A4000_1.Value = 0.07;
if value == 'Brick, unglazed, painted'
app.A125_1.Value = 0.01
app.A250_1.Value = 0.01
app.A500_1.Value = 0.02
app.A1000_1.Value = 0.02
app.A2000_1.Value = 0.03
app.A4000_1.Value = 0.04;
if value == 'Carpet 3.2mm pile height'
app.A125_1.Value = 0.05
app.A250_1.Value = 0.05
app.A500_1.Value = 0.10
app.A1000_1.Value = 0.20
app.A2000_1.Value = 0.30
app.A4000_1.Value = 0.40;
end
end
Second Failed attempt: % Value changed function: Material1DropDown function Material1DropDownValueChanged(app, event) value = app.Material1DropDown.Value app.Mat1.Value = app.Material1DropDown.Value;
if value == 'Brick, unglazed'
app.A125_1.Value = 0.03
app.A250_1.Value = 0.03
app.A500_1.Value = 0.03
app.A1000_1.Value = 0.04
app.A2000_1.Value = 0.05
app.A4000_1.Value = 0.07;
elseif value == 'Brick, unglazed, painted'
app.A125_1.Value = 0.01
app.A250_1.Value = 0.01
app.A500_1.Value = 0.02
app.A1000_1.Value = 0.02
app.A2000_1.Value = 0.03
app.A4000_1.Value = 0.04;
else value == 'Carpet 3.2mm pile height'
app.A125_1.Value = 0.05
app.A250_1.Value = 0.05
app.A500_1.Value = 0.10
app.A1000_1.Value = 0.20
app.A2000_1.Value = 0.30
app.A4000_1.Value = 0.40;
end
end
<<
>>

Accepted Answer

Ahmet Cecen
Ahmet Cecen on 24 Mar 2018
Edited: Ahmet Cecen on 24 Mar 2018
Ok, first of all something that should have been immediately visible that I didn't notice before failing to run your app is that your if statements are constructed incorrectly. It should be instead:
if value == 'Brick, unglazed'
app.A125_1.Value = 0.03
app.A250_1.Value = 0.03
app.A500_1.Value = 0.03
app.A1000_1.Value = 0.04
app.A2000_1.Value = 0.05
app.A4000_1.Value = 0.07;
elseif value == 'Brick, unglazed, painted'
app.A125_1.Value = 0.01
app.A250_1.Value = 0.01
app.A500_1.Value = 0.02
app.A1000_1.Value = 0.02
app.A2000_1.Value = 0.03
app.A4000_1.Value = 0.04;
elseif value == 'Carpet 3.2mm pile height'
app.A125_1.Value = 0.05
app.A250_1.Value = 0.05
app.A500_1.Value = 0.10
app.A1000_1.Value = 0.20
app.A2000_1.Value = 0.30
app.A4000_1.Value = 0.40;
end
Notice the "elseif". This may very well be the only problem, but let me know if something else pops up.
A little information on why the error says improper nesting: every time you use "if" there needs to be a corresponding "end". Since you are using 3 "if" s there but only closing one of them, MATLAB thinks that the next function in the list (function Material2DropDownValueChanged(app, event)) is nested inside the the precious function's "if", which is not allowed.
  2 Comments
Ahmet Cecen
Ahmet Cecen on 24 Mar 2018
Edited: Ahmet Cecen on 24 Mar 2018
Ok another thing is use:
strcmp(value,'Brick, unglazed')
the == sign is causing interpretation problems it seems, which is the cause of the dimension mismatch error. MATLAB is treating value(char) as an array of characters, which means == sign is trying to do element-wise comparison so:
value == 'Brick, unglazed'
Matrix dimensions must agree.
while when they are the same length:
value == 'Carpet, 7.2mm pile height'
ans =
1×25 logical array
1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
but:
strcmp(value,'Brick, unglazed')
ans =
logical
0
and:
strcmp(value,'Carpet, 3.2mm pile height')
ans =
logical
1

Sign in to comment.

More Answers (0)

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!