MATLAB functions has similar name with different numbers
1 view (last 30 days)
Show older comments
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.
0 Comments
Answers (2)
Walter Roberson
on 31 Oct 2017
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.
0 Comments
Image Analyst
on 31 Oct 2017
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)
0 Comments
See Also
Categories
Find more on Interactive Control and Callbacks in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!