-If else- conditionals in matlab app.designer?
93 views (last 30 days)
Show older comments
ErikJon Pérez Mardaras on 16 Jan 2021
Commented: NICOLE MIN on 22 Apr 2021
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!
Steven Lord on 16 Jan 2021
'ac' == 'ad' % returns a 1-by-2 logical array
'ac' == "ad" % returns a 1-by-1 logical array
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 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.
You can use a single &, and your code will run. Better is to use strcmp, though.
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.
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!