Creating table of toggle buttons

1 次查看(过去 30 天)
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