필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Off state of checkbox not being recognized in another function

조회 수: 2 (최근 30일)
Harold
Harold 2012년 2월 13일
마감: MATLAB Answer Bot 2021년 8월 20일
I have a check box that I'm having problems with passing the value of.
When the checkbox is clicked, I set col_add=1. If the checkbox is not clicked I set col_add=0;
I then pass col_add to the handle of the checkbox which is used in another function.
Basically, if the checkbox is selected, I add a column to the input of a textbox. If it is not, then the table uses the input from the textbox for the number of columns.
The problem that I'm having is that initially the checkbox is unselected and if I accept this option, it doesn't set col_add=0. Instead the value is 12.044. The line that seems to give this number is col_add=handles.nonuniform_weights, which I don't understand since I set this value to 0 if the checkbox was unchecked. Here is the warning I get...Warning: Size vector should be a row vector with integer elements. The code is shown below.
nonuniform_weights: checkbox
% --- Executes on button press in nonuniform_weights.
function nonuniform_weights_Callback(hObject, eventdata, handles)
if (get(hObject,'Value') == get(hObject,'Min'))
col_add=0;
elseif (get(hObject,'Value') == get(hObject,'Max'))
col_add=1;
end
handles.nonuniform_weights = col_add;
guidata(hObject,handles);
% --- Executes on button press in pnt_accept.
function pnt_accept_Callback(hObject, eventdata, handles)
num = findobj(gcf,'Tag','contrl_pts_num');
contrl_pts_num = str2num(get(num,'String'));
col_add=handles.nonuniform_weights;
if col_add==1
cnames = {'X-Data','Y-Data','Z-Data', 'Weights'};
columneditable = [true true true true];
elseif col_add==0
cnames = {'X-Data','Y-Data','Z-Data'};
columneditable = [true true true];
end
f = figure('Position',[200 200 400 550]);
dat = zeros(contrl_pts_num,3+col_add);
t = uitable('Parent',f,'Data',dat,'ColumnName',cnames,'Position',[20 20 360 500],'ColumnEditable', columneditable);

답변 (1개)

Walter Roberson
Walter Roberson 2012년 2월 13일
If you have nonuniform_weights_Callback and you are using GUIDE (as you appear to be) then the implication is that you have a button named nonuniform_weights and that the handle for that button is stored automatically in handles.nonuniform_weights . But then you try to use that exact same structure element name to store a value, and that dual use of the same structure element is causing problems.
You could consider using the UserData field to store the value. Or you could leave out the nonuniform_weights_Callback and simply calculate the col_add on the fly as
col_add = get(handles.nonuniform_weights,'Value') == get(handles.nonuniform_weights,'Max');
  댓글 수: 1
Harold
Harold 2012년 2월 13일
Thank you, that col_add on the fly worked perfectly. I'm a beginner with programming GUI's in MATLAB.

이 질문은 마감되었습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by