Info

This question is closed. Reopen it to edit or answer.

Off state of checkbox not being recognized in another function

1 view (last 30 days)
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);

Answers (1)

Walter Roberson
Walter Roberson on 13 Feb 2012
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 Comment
Harold
Harold on 13 Feb 2012
Thank you, that col_add on the fly worked perfectly. I'm a beginner with programming GUI's in MATLAB.

This question is closed.

Community Treasure Hunt

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

Start Hunting!