フィルターのクリア

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