Change "Edit" uicontrols background (how to set a structure array element to a specific value?)

I have a row of "edit" uicontrols. The handles of these are in a vector (e.g. in c). I want to change the background color of each "edit" uicontrol.
hfig=figure(1);
clf;
c(1) = uicontrol(hfig...
,'Style','edit'...
,'Position',[20 20 60 20]...
);
c(2) = uicontrol(hfig...
,'Style','edit'...
,'Position',[20 50 60 20]...
);
If I try to change the background color
c.BackgroundColor= [1 0 0]
c(:).BackgroundColor= [1 0 0]
gives the same error:
"Expected one output from a curly brace or dot indexing expression, but there were 2 results."
Is it possible to do it this or similar way, or just the for loop is the solution? I would be happy if logical indexing would work changing just special edit boxes' Foreground!

 Accepted Answer

set(c, 'BackGroundCOlor', [1 0 0])

5 Comments

Yes!!!!
Thank you.
AND, the logical indexing works tooo!
Great!
Csaba
However, it does not work for a user definied structure.
a(1).d=1;
a(2).d=3;
set(a,'d',22)
gives the error
Error using set
Conversion to double from struct is not possible.
So it works for object arrays but not for user definied structure arrays.
arrayfun(@(V) setfield(V, 'd', 22), a) %works on structs
arrayfun(@(V) setfield(V, 'BackgroundColor', [1 0 0]), c) %works on objects
Sorry Walter, it does not work on structures. It also works on objects, but does not on user definied structures.
After
arrayfun(@(V) setfield(V, 'd', 22), a) %works on structs
a.d
ans =
1
ans =
3
a = arrayfun(@(V) setfield(V, 'd', 22), a);
setfield() returns a new structure with the changes, but does not change any variable passed into it.
setfield() works to change graphics objects without assignment because graphics objects are handle objects.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2019b

Community Treasure Hunt

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

Start Hunting!