Serious error in transferring strings from one listbox to another

1 view (last 30 days)
[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

Accepted Answer

Fangjun Jiang
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.
  1 Comment
William
William on 16 Jun 2011
I did that but I cannot get any good info out of the error. Here it is
??? Error using ==> horzcat
CAT arguments dimensions are not consistent.
Error in ==> MAT_to_Csv>select_button_Callback at 238
newmenu = [choice_listbox1, update_listbox2];
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> MAT_to_Csv at 42
gui_mainfcn(gui_State, varargin{:});
??? Error while evaluating uicontrol Callback

Sign in to comment.

More Answers (2)

Walter Roberson
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
William on 16 Jun 2011
I fixed it! Thanks
  4 Comments
Giorgi
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.

Sign in to comment.

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!