필터 지우기
필터 지우기

Creating table of toggle buttons

조회 수: 2 (최근 30일)
Luis Chaves
Luis Chaves 2019년 2월 23일
댓글: Krishna Zanwar 2019년 3월 1일
Hello!
I am trying to create a table of toggle buttons so that when a button is pressed the value of that cell in an equivalent matrix to the table is changed. My question is: Do I need one callback function for each button or can I use a single function that handles the pressing/unpressing of any button in the table? If I need one callback function per button what is the best way to do so? Can I define callback function in the loops that build the table?
Here is my code (the callback function is not yet built):
S.fh = figure('units','pixels',...
'position',[200 200 30*12 30*8],...
'menubar','none',...
'numbertitle','off',...
'name','Well selection',...
'resize','on');
count = 1;
for a = 1:12
for b = 1:8
S.pb(a,b) = uicontrol('style','toggle',...
'units','pixels',...
'position',[30*(a-1) 240-30*b 30 30],...
'fontsize',14,...
'string',num2str(count),...
%The callback function is below, Would it be possible to define this callback function iteratively?
'callback',{@pb_call,S});
count = count +1;
end
end

답변 (1개)

Krishna Zanwar
Krishna Zanwar 2019년 2월 27일
Hey Luis,
A single function can be used for callback of all the toggle buttons.
The button which was pressed can be recognized by-
get(hObject,'String')
The value of the button that was pressed can be displayed by-
get(hObject,'Value')
The pb_call function will be called when any button is pressed.
  댓글 수: 2
Luis Chaves
Luis Chaves 2019년 3월 1일
Hello Krishna,
Thank you for your reply, I am not very familiar with the use of hObject, I am using the structure S to save everything related to the pushbuttons, would I need to create S.hObject?
Could you provide some sample code of what the callback function would look like?
Best,
Luis
Krishna Zanwar
Krishna Zanwar 2019년 3월 1일
Add this to your code and it will save the string of toggle button in 'Number' and the value of that button in 'Value'
function pb_call(hObject,a,b)
Number=get(hObject,'String')
Value=get(hObject,'Value')
end
You should get more information about callbacks here.
Hope it helps.

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

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by