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

7 次查看(过去 30 天)
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
<<
>>

采纳的回答

Ahmet Cecen
Ahmet Cecen 2018-3-24
编辑:Ahmet Cecen 2018-3-24
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 个评论
Ahmet Cecen
Ahmet Cecen 2018-3-24
编辑:Ahmet Cecen 2018-3-24
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

请先登录,再进行评论。

更多回答(0 个)

类别

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