-If else- conditionals in matlab app.designer?
66 views (last 30 days)
ErikJon Pérez Mardaras on 16 Jan 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.