Getting error when using Listbox
조회 수: 1 (최근 30일)
이전 댓글 표시
For the codes below, results are coming and the codes are running, however some error message (attached alongwith) does come in (but it does not stop the process). Have attached the screeshot of error. Plz guide.
function listbox1_Callback(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns listbox1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from listbox1
% Get value from edit field 1
% Get value from edit field 1
b = str2double(handles.edit1.String);
% Get list of everything in the listbox.
listboxItems = handles.listbox1.String;
% Get the index of the item they selected.
selectedItem = handles.listbox1.Value;
% Turn that selection into a double number.
listboxNumber = str2double(listboxItems{selectedItem});
listboxString = listboxItems{selectedItem};
if strcmpi (listboxString,'None')
set(handles.edit1,'String','');
set(handles.edit2,'String','');
handles.edit1.Enable ='Off';
handles.edit2.Enable = 'Off';
elseif listboxNumber == 1.5
handles.edit1.Enable = 'On';
handles.edit2.Enable = 'On';
c = 20;
elseif listboxNumber == 2
handles.edit1.Enable = 'On';
handles.edit2.Enable = 'On';
c = 30;
end
%c = str2double(c);
% Do the multiplication.
d = b * c;
% Send the result into edit field 3.
handles.edit2.String = sprintf('%.4f', d);
댓글 수: 0
답변 (1개)
Walter Roberson
2021년 3월 15일
You do not always set the variable c but you always need it to be defined. In particular you have a problem when None is the chosen listbox entry, or if listboxNumber is not 1.5 or 2, or if the user had not selected anything in the list box (unless you are careful, if nothing has been selected in the listbox yet, then Value is empty.)
댓글 수: 3
Walter Roberson
2021년 3월 15일
c = - inf;
before the if sequence. Then if your code fails to change c to something else then the code will not crash, but will output -inf instead, signaling you that you missed a case in your logic.
참고 항목
카테고리
Help Center 및 File Exchange에서 Environment and Settings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!