MATLAB functions has similar name with different numbers

1 次查看(过去 30 天)
Hello there, I am new in Matlab. I am trying to develop a GUI. In the interface I have some checkboxes and each has a callback. The tag of the checkboxes has similar name with different numbers, like checkbox1, checkbox2,checkbox3... Correspondingly the callback function is named checkbox1_Callback, checkbox2_Callback, checkbox3_Callback... Now I want to check the active checkbox. If the checkbox is activated then call the callback function. Here are some of my code
INDEX = [75,74,76,77,73,78,70,71,72,68,69,81,82,83,79,80,86,85,84,87]; % the numbers are the checkboxes numbers
for i = 1:1:numel(INDEX)
if get(handles.(['checkbox',num2str(INDEX(i))]),'value')==1
(['checkbox',num2str(INDEX(i)),'_Callback']);
end
end
I set some breakpoints and the condition is correct, but it seems like I get a char type other than call the function. Anybody can help me. Thanks very much.

回答(2 个)

Walter Roberson
Walter Roberson 2017-10-31
Why not use the Callback property instead?
You could also consider using uibuttongroup() to manage the boxes -- though really it would want toggle buttons instead of checkboxes.

Image Analyst
Image Analyst 2017-10-31
So do the different checkboxes each have completely different code in their callback functions? Like you have 20 totally different codes? Or all 20 checkboxes do the same thing, like gray out a label or disable a button or something? Also, what function is calling those 20 callbacks? Are the callbacks of checkboxes 1, 2, and 3 all going to check the values of checkboxes 75 through 87 and execute the callbacks of whichever were checked?
Why not just have one function, called ProcessAllCheckboxes(handles) or something and in there do stuff?
function handles = ProcessAllCheckboxes(handles)
if handles.checkbox75.Value
% Do something
end
if handles.checkbox76.Value
% Do something
end
if handles.checkbox77.Value
% Do something
end
etc.
The "something" could be the same single function, or it could be totally different code. Each checkbox's callback could simply call this same function, for example, for checkbox1:
function checkbox1_Callback(hObject, eventdata, handles)
handles = ProcessAllCheckboxes(handles)

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by