Serious error in transferring strings from one listbox to another
2 次查看(过去 30 天)
显示 更早的评论
[EDIT: 20110616 11:18 CDT - reformat - WDR]
I am trying to create two list boxes. One will list the contents of a .MAT file, the other will hold the selected variable files that I choose when I select the variables in the first list box and hit "Select" they will move into the other list box. I am trying to update the number of items in listbox2 and keep crashing matlab. here is the code:
% --- Executes on button press in select_button.
function select_button_Callback(hObject, eventdata, handles)
% hObject handle to select_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
list_entry = get(handles.listbox1,'String');
index_selected = get(handles.listbox1,'Value');
choice_listbox1 = list_entry(index_selected);
update_listbox2 = get(handles.listbox2, 'string');
newmenu = [choice_listbox1, update_listbox2];
% for newmenulist = 1:length(newmenu)
% listofvars2 = [listofvars2 )];
% end
set(handles.listbox2,'String', newmenu);
If I have three variables and add the first two nothing happens but adding the third crashes it. I have no idea why.
Thank you for your help
Bill
采纳的回答
Fangjun Jiang
2011-6-16
The easiest thing to do is to put a break point in your code and step it through to see where and when the error happened. I suspect the line newmenu = [choice_listbox1, update_listbox2] has problem with data type, string or cell. Pay attention to that line. Once you know the line where error happens, the next time you can pause there, copy and run the line at the command window to see the error message. That way, the error will not cause your debugging to stop.
更多回答(2 个)
Walter Roberson
2011-6-16
% --- Executes on button press in select_button.
function select_button_Callback(hObject, eventdata, handles)
% hObject handle to select_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
list_entry = cellstr(get(handles.listbox1,'String'));
index_selected = get(handles.listbox1,'Value');
choice_listbox1 = list_entry(index_selected);
update_listbox2 = cellstr(get(handles.listbox2, 'string'));
newmenu = [choice_listbox1, update_listbox2];
set(handles.listbox2,'String', newmenu);
That is, you are likely having a conflict with listbox String defaulting to '' (empty string) when you want to be working with cells. Also, some ways of initializing listbox string can result in char arrays rather than cell arrays. It is safer to cellstr() to be sure you have cell arrays.
Question: your code puts the newly selected variable at the top of the list. Is that what you want?
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Entering Commands 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!