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

Respuestas (1)

Walter Roberson
Walter Roberson el 15 de Mzo. de 2021

0 votos

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 comentarios

Avinav Kumar
Avinav Kumar el 15 de Mzo. de 2021
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 el 16 de Mzo. de 2021
Thanks a lot Walter. It works.

Iniciar sesión para comentar.

Categorías

Más información sobre Programming en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 15 de Mzo. de 2021

Comentada:

el 16 de Mzo. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by