Getting error when using Listbox

3 visualizzazioni (ultimi 30 giorni)
Avinav Kumar
Avinav Kumar il 15 Mar 2021
Commentato: Avinav Kumar il 16 Mar 2021
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);

Risposte (1)

Walter Roberson
Walter Roberson il 15 Mar 2021
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 Commenti
Walter Roberson
Walter Roberson il 15 Mar 2021
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 il 16 Mar 2021
Thanks a lot Walter. It works.

Accedi per commentare.

Categorie

Scopri di più su Environment and Settings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by