How can I change pushbutton background

조회 수: 2 (최근 30일)
Tarcisio
Tarcisio 2014년 12월 10일
답변: Daniel 2014년 12월 10일
I constructed a number of pushbuttons by using the below loop, and now I want to change the background of each one when it was pressed.
for i = 1 : 8
for j = 1 : 7
btn(j,i) = uicontrol('Parent', figure_handle, ...
'Style', 'pushbutton', ...
'Position', [45+(i*51) 552-(j*50) 52 51],...
'Callback', @changeBG);
end
end
How can i get the correct button to pass to changeBG function? Can someone help me?
Thank you

채택된 답변

Orion
Orion 2014년 12월 10일
I'd say :
for i = 1 : 8
for j = 1 : 7
btn(j,i) = uicontrol('Parent', figure_handle, ...
'Style', 'pushbutton', ...
'Position', [45+(i*51) 552-(j*50) 52 51],...
'Callback', {@changeBG,i,j}); % add too arguments to changeBG
end
end
then in changeBG, something like
function changeBG(hobject,evendata,i,j)
if i==1 && j==1
set(hobject,'Backgroundcolor','r') % red for i=1, j=1
elseif ....
% same kind of treatment for each of your button
elseif ....
elseif ....
elseif ....
end
  댓글 수: 1
Tarcisio
Tarcisio 2014년 12월 10일
편집: Tarcisio 2014년 12월 10일
It works, but now I'm trying to pass a value of a matrix to the function. And I'm getting strange values:
[R,C] = size(matrix);
for i = 1 : C
for j = 1 : R
btn(j,i) = uicontrol('Parent', figure_handle, ...
'Style', 'pushbutton', ...
'Position', [45+(i*lColumn) 552-(j*lRow) 52 51],...
'Callback', {@changeBG,matrix(j,i),i,j});
end
end
I'm getting numbers like 50, 51, 48 ... and my matrix only have numbers between 0 - 7.

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

추가 답변 (1개)

Daniel
Daniel 2014년 12월 10일
If you mean the background as a whole and not just the color... I normally perform this task by modifying the "cdata" of that pushbutton. That allows you for example to use images as pushbutton background. Be careful by defining the size of the images so they fit properly inside the pushbutton you defined.
Hope it helps!

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by