hObject error in execution of a checkbox

조회 수: 1 (최근 30일)
Vicky S
Vicky S 2019년 4월 12일
댓글: Vicky S 2019년 4월 12일
I have been trying to execute the below mentioned code and encounter the below mentioned error
function checkbox()
handles.cbh = zeros(40,1);
handles.label = 1:40;
for k = 1:40
handles.cbh(k) = uicontrol('Style','checkbox','String',handles.label(k), ...
'Value',0,'Position',[30 20*k 130 20], ...
'Parent', hObject, ...
'Callback',{@checkBoxCallback,k});
end
guidata(hObject,handles);
function checkBoxCallback(hObject,~,checkBoxId)
handles = guidata(handles);
value = get(hObject,'Value');
if value
switch checkBoxId
case 1
fprintf('handle cb 1\n');
case 2
fprintf('handle cb 2\n');
otherwise
fprintf('do nothing\n');
end
end
end
end
the error is
Undefined function or variable 'hObject'.
Error in checkbox (line 5)
handles.cbh(k) = uicontrol('Style','checkbox','String',handles.label(k), ...

채택된 답변

Dennis
Dennis 2019년 4월 12일
Your checkbox function does not know 'hObject':
function checkbox()
handles.fig=figure;
handles.cbh = zeros(40,1);
handles.label = 1:40;
for k = 1:40
handles.cbh(k) = uicontrol('Style','checkbox','String',handles.label(k), ...
'Value',0,'Position',[30 20*k 130 20], ...
'Parent', handles.fig, ...
'Callback',{@checkBoxCallback,k});
end
guidata(handles.fig,handles);
function checkBoxCallback(hObject,~,checkBoxId)
handles = guidata(hObject);
value = get(hObject,'Value');
if value
switch checkBoxId
case 1
fprintf('handle cb 1\n');
case 2
fprintf('handle cb 2\n');
otherwise
fprintf('do nothing\n');
end
end
end
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

제품


릴리스

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by