MATLAB functions has similar name with different numbers

2 Ansichten (letzte 30 Tage)
Penny
Penny am 31 Okt. 2017
Beantwortet: Image Analyst am 31 Okt. 2017
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.

Antworten (2)

Walter Roberson
Walter Roberson am 31 Okt. 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.

Image Analyst
Image Analyst am 31 Okt. 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)

Kategorien

Mehr zu Interactive Control and Callbacks finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by