Serious error in transferring strings from one listbox to another
1 view (last 30 days)
Show older comments
[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
1 Comment
Accepted Answer
Fangjun Jiang
on 16 Jun 2011
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.
More Answers (2)
Walter Roberson
on 16 Jun 2011
% --- 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 Comments
See Also
Categories
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!