Code checking error in App Designer, it thinks xxxx.BackgroundColor is a scalar

I'm changing the background color to alert the user about various (async) conditions and button pressed.
if app.STOPTRIALSButton.BackgroundColor == [0.95 0.33 0.10]
app.STOPTRIALSButton.BackgroundColor = [0 1 0];
end
and I get the warning:
Unexpected use of '[' in a scalar context.
but of course colors are RGB three values, so the code runs fine.
Also while the Component browser shows (another) color as 0.65, 0.65, 0.65 that's the wrong syntax (no commas please for easier cut and paste) and in the startup code the values are actually [0.651 0.651 0.651] so my compare failed without significant research as to why.
Finally, App Designer isn't listed as a product below, so I can't flag that. Just saying MATLAB is the product.

답변 (2개)

To avoid the warning, use:
if isequal(app.STOPTRIALSButton.BackgroundColor,[0.95 0.33 0.10])
app.STOPTRIALSButton.BackgroundColor = [0 1 0];
end

댓글 수: 3

except [0.95 0.33 0.10] doesn't work since even though those are the numbers given by the Component Browser, App Designer puts in code like this:
app.STOPTRIALSButton.BackgroundColor = [0.949 0.3294 0.102] so isequal doesn't work
(unless you can set an epsilon where isequal() is sloppy to 2 decimal rounding
Numeric inputs are equivalent if they are the same size and their contents are of equal value.
I know that converting from float to decimal strings is yucky so how about App Designer always rounding colors to 2 decimal places (or use 0-255 like everyone else does) since internally its really bytes anyway above color being 242 84 26
242/255 = 0.949 and 84/255 = 0.3294117647058824 if you look far enough it will never match
You could also go 0 to 100 (%) for those who don't like binary based numbers, lower precision but good enough. OR at least put a warning in the help about how to find equal colors.
searching equal colors is of no help.
Do not compare floating point numbers for exact equality.
if isnumeric(app.STOPTRIALSButton.BackgroundColor) && all(abs(app.STOPTRIALSButton.BackgroundColor - [0.95 0.33 0.10]) <= 0.01)
If App Designer puts a line of code that says
app.STOPTRIALSButton.BackgroundColor = [0.949 0.3294 0.102]
and you want to check that that BackgroundColor is still in effect, use
if isequal(app.STOPTRIALSButton.BackgroundColor,[0.949 0.3294 0.102])

댓글을 달려면 로그인하십시오.

Alessandro Livi
Alessandro Livi 2024년 7월 10일
This is crazy that you can't simply compare colors, consder options I mention above meanwhie
if isnumeric(app.STOPTRIALSButton.BackgroundColor) && all(abs(app.STOPTRIALSButton.BackgroundColor - [0.95 0.33 0.10]) <= 0.01
should work,
(make this a documented (or included) function SameColor( Color1 Color2) as above?
Also I was directed to a colors names list (not in MatLab) and most of the names don't work.
'color name' must match a rather short list, none of the off white names work (like a name for [0.94 0.94 0.94] or [0.90 0.90 0.90] which seem to be common default background colors

댓글 수: 1

Here's the list of named colors in MATLAB
They are the eight colors all of whose RGB components are 0 or 1 (i.e. [0,0,0], [0,0,1], [0,1,0], [0,1,1], [1,0,0], [1,0,1], [1,1,0], [1,1,1]).

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품

릴리스

R2023b

질문:

2024년 7월 6일

댓글:

2024년 7월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by