Serious error in transferring strings from one listbox to another
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
Elysi Cochin
on 24 Oct 2012
how did u fix it... i'm facing the same pblm...
Accepted Answer
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?
William
on 16 Jun 2011
0 votes
4 Comments
Shristi
on 13 Jul 2011
Hey William,
Do you remember how you fixed this issue. I am trying to do the same thing. It works only for the first time transfer, but not after that. HELP!
Shristi
on 13 Jul 2011
I figured it out too. Thanks to your post.:-)
Giorgi
on 26 Nov 2014
Hi guys! Thats perfect that you fixed it buut i have the same problem so please if you can explain the explanations in details.
Giorgi
on 26 Nov 2014
OH good i fixed it !
Categories
Find more on Entering Commands 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!