Getting error when using Listbox

3 次查看(过去 30 天)
Avinav Kumar
Avinav Kumar 2021-3-15
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
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
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 CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by