Code checking error in App Designer, it thinks xxxx.BackgroundColor is a scalar
48 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(2 个)
Voss
2024-7-6
To avoid the warning, use:
if isequal(app.STOPTRIALSButton.BackgroundColor,[0.95 0.33 0.10])
app.STOPTRIALSButton.BackgroundColor = [0 1 0];
end
3 个评论
Walter Roberson
2024-7-7
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)
Voss
2024-7-7
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
2024-7-10
1 个评论
Voss
2024-7-10
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]).
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Develop uifigure-Based Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!