Getting error when using Listbox

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);

답변 (1개)

Walter Roberson
Walter Roberson 2021년 3월 15일

0 개 추천

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

Avinav Kumar
Avinav Kumar 2021년 3월 15일
I tried a lot many ways, but error still coming. Can u plz suggest some way out.
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.
Avinav Kumar
Avinav Kumar 2021년 3월 16일
Thanks a lot Walter. It works.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

태그

질문:

2021년 3월 15일

댓글:

2021년 3월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by