Background Color of a Pushbuton

Hi! I have an array of 5 elements of 1's and 0's, for example A[0,0,0,1,1] and I have 5 pushbuttons I created. What I want to do is have a simple loop that checks for the ones and zeros in my array and change the corresponding background color of my pushbutton. Right now I want them to change to green if it is a '0' and red if it is a '1'. I am just unfamiliar with Matlab syntax, here's my thought process:
For(int i=0, i<6, i++){ if(A[i]==1) set(pushbutton + i, 'backgroundcolor', 'red') else set(pushbutton + i, 'backgroundcolor', 'green')}
any hints on what i'm dong wrong would be greatly appreciated!

2 Comments

Update: here's a better idea of what i'm trying to do but I can't seem to figure out how to iterate through a loop and modify a corresponding pushbutton: i=1, pushbutton1; i=2,pushbutton2 etc..heres what i tried but it doesn't like my pushbutton tag reference
function parkinglot_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to parkinglot (see VARARGIN)
A = [0 0 0 1 1];
for i = 1:5
if A(i) == 1
set(handles.(pushbutton + i), 'backgroundcolor', 'green');
else
set(handles.(pushbutton + i), 'backgroundcolor', 'red');
end
end
Update: figured it out did the following:
function parkinglot_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to parkinglot (see VARARGIN)
A = [0 0 0 1 1];
pushbutton = [handles.pushbutton1,handles.pushbutton2,handles.pushbutton3,handles.pushbutton4,handles.pushbutton5];
for i = 1:5
if A(i) == 1
set((pushbutton(i)), 'backgroundcolor', 'green');
else
set((pushbutton(i)), 'backgroundcolor', 'red');
end
end
% Choose default command line output for parkinglot
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);

Sign in to comment.

Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Asked:

on 27 Apr 2015

Commented:

on 28 Apr 2015

Community Treasure Hunt

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

Start Hunting!