Help Solve Reset Listbox Error?

2 次查看(过去 30 天)
I have a script that resets two secondary listboxes depending on the value selected from the primary listbox.
Here is a code snippet:
% --- Executes on selection change in toplistbox.
function toplistbox_Callback(hObject, eventdata, handles,output_index)
% hObject handle to toplistbox (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 toplistbox contents as cell array
% contents{get(hObject,'Value')} returns selected item from toplistbox
set(handles.Answer,'String',' ');
calculation=get(hObject,'value');
handles.top_index=calculation;
if(calculation==1) % natural frequency
clear string;
set(handles.left_text,'String','Mass');
string{1}='lbm';
string{2}='lbf sec^2/in';
string{3}='kg';
set(handles.leftlistbox,'String',string)
set(handles.right_text,'String','Stiffness');
clear string;
string{1}='lbf/in';
string{2}='lbf/ft';
string{3}='N/m';
string{4}='N/mm';
set(handles.rightlistbox,'String',string)
end
if(calculation==2) % mass
set(handles.left_text,'String','Natural Frequency');
clear string;
string{1}='Hz';
string{2}='rad/sec';
set(handles.leftlistbox,'String',string)
set(handles.right_text,'String','Stiffness');
clear string;
string{1}='lbf/in';
string{2}='lbf/ft';
string{3}='N/m';
string{4}='N/mm';
set(handles.rightlistbox,'String',string)
end
if(calculation==3) % stiffness
set(handles.left_text,'String','Natural Frequency');
clear string;
string{1}='Hz';
string{2}='rad/sec';
set(handles.leftlistbox,'String',string)
set(handles.right_text,'String','Mass');
clear string;
string{1}='lbm';
string{2}='lbf sec^2/in';
string{3}='kg';
set(handles.rightlistbox,'String',string)
end
handles.toplistbox = get(hObject,'value');
guidata(hObject, handles);
  • * *The script runs perfectly fine if I run it and exercise various permutations of options a dozen or so times within the run. But eventually, I get an error message such as:
Error using handle.handle/set Invalid or deleted object.
Error in sdof_fn>toplistbox_Callback (line 162) set(handles.leftlistbox,'String',string)
So after a number of times, the leftlistbox becomes invalid. Please help. The full script and figure is available at:
www.vibrationdata.com/matlab2/sdof_fn.zip
Thank you, Tom Irvine

采纳的回答

Tom Irvine
Tom Irvine 2012-12-11
Thank you Image Analyst.
I found the error. I was inadvertently deleting the secondary listboxes in other Callback functions due to a programmer inexperience/nuance.
Thank you, Tom

更多回答(1 个)

Image Analyst
Image Analyst 2012-12-11
编辑:Image Analyst 2012-12-11
handle.handle??? Set "dbstop if error" and examine what handles is when it stops. You could have a problem if the listbox has item 3 selected and then you make it into a list that has only 3 items. Best case the listbox will vanish, worst case it will throw an exception. Make sure you set the value property to 1 before you set the string property.

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by